Code Tu Tam

Hàm ctype_cntrl() trong PHP là gì ?

Rate this post

Hàm ctype_cntrl() trong PHP có nhiệm vụ kiểm tra các ký tự trong chuỗi có là ký tự điều khiển như line feed, tab, escape không.

Hàm tương thích với PHP 4, 5, 7.

Cú pháp

Cú pháp hàm ctype_cntrl() trong PHP như sau:

ctype_cntrl ( string $text ) : bool

Tham số truyền vào 

Giá trị trả về

Hàm này trả về TRUE nếu tất cả ký tự trong chuỗi là ký tự điều khiển, ngược lại sẽ trả về FALSE. Nếu trong chuỗi có lẫn các ký tự khác thì hàm sẽ trả về FALSE.

Ví dụ minh họa 1

<?php
$strings = array('string1' => "\n\r\t", 'string2' => 'arf12');
foreach ($strings as $name => $testcase) {
    if (ctype_cntrl($testcase)) {
        echo "The string '.$name.' consists of all control characters.\n";
    } else {
        echo "The string '.$name.' does not consist of all control characters.\n";
    }
}
?>
The string 'string1' consists of all control characters.
The string 'string2' does not consist of all control characters.

Ví dụ minh hoạ 2

<?php
$str1 = "\r\n";
if(ctype_cntrl($str1))
echo ("str1 contains all control characters.\n");
else
echo ("str1 does not contain all control characters.\n");
$str2 = "\t\x12";
if(ctype_cntrl($str2))
echo ("str2 contains all control characters.\n");
else
echo ("str2 does not contain all control characters.\n");
$str3 = "\x00\x12\x1f\x7f";
if(ctype_cntrl($str3))
echo ("str3 contains all control characters.\n");
else
echo ("str3 does not contain all control characters.\n");
$str4 = "\r \n";
// space is there 
if(ctype_cntrl($str4))
echo ("str4 contains all control characters.\n");
else
echo ("str4 does not contain all control characters.\n");
$str5 = "Hello123";
// alphabets & digits are there
if(ctype_cntrl($str5))
echo ("str5 contains all control characters.\n");
else
echo ("str5 does not contain all control characters.\n");
?>


str1 contains all control characters.
str2 contains all control characters.
str3 contains all control characters.
str4 does not contain all control characters.
str5 does not contain all control characters.

Các hàm liên quan

Hi vọng với bài viết này, bạn đã hiểu rõ cách sử dụng hàm ctype_cntrl() trong PHP. Nếu thấy bài viết hay và ý nghĩa, hãy like và chia sẻ với bạn bè để mọi người cùng nhau học tập nhé. Cảm ơn bạn đã ghé thăm codetutam.com

Exit mobile version