PHP | ctype_cntrl() Function

The ctype_cntrl() is an inbult function in PHP which is used to check all the characters in string/text are control characters. Control characters are e.g. line feed, tab, escape.

Syntax:

bool ctype_cntrl ( $str )

Parameters: This function accepts a single parameter $str. It is a mandatory parameter which specifies the string.



Return Value: It returns True if a string contains only control characters and False on failure.

Examples:

Input: GeeksforGeeks
Output: No
Explanation: String (GeeksforGeeks) contains only the alphanumeric characters.

Input: \n\t
Output: Yes
Explanation: String (\n\t) contains only the control character.

Below programs illustrate the ctype_cntrl() function.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check if a string has all
// control characters
  
$str1 = "GeeksforGeeks";
  
    if ( ctype_cntrl($str1)) 
      
        echo "Yes\n";
    else
        echo "No\n";
  
$str2 = "\n\t";
  
    if ( ctype_cntrl($str2)) 
      
        echo "Yes\n";
    else
        echo "No\n";
?>

chevron_right


Output:

No
Yes

Program 2: Implementation of ctype_cntrl() function which takes input of an array of string that contains integers and special Symbol.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check if a string has all
// control characters
   
$str = array(
    "Geeks",
    "Geeks space",
    "@@##-- /",
    "\n",
    "\t \r",
    "\r\t\n"
);
   
// Check the above strings by using
// ctype_cntrl() function
foreach ($str as $test) {
       
    if (ctype_cntrl($test))
        echo "Yes\n";
    else
        echo "No\n";    
}
   
?>

chevron_right


Output:

No
No
No
Yes
No
Yes

References: http://php.net/manual/en/function.ctype-cntrl.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.