Home » PHP » PHP Ctype functions

PHP ctype_cntrl() function with example

PHP ctype_cntrl() function: Here, we are going to learn about the ctype_cntrl() function with example in PHP.
Submitted by IncludeHelp, on February 04, 2019

PHP ctype_cntrl() function

ctype_cntrl() function is a character type (CType) function in PHP, it is used to check whether a given string contains all control characters or not.

It returns true if all characters of the given strings are control characters (like, a newline character, tab character, escape character etc). Else it returns false.

Note: Though control characters are unprintable character i.e. they cannot be represented in the string format if we represent they may display like symbols. So, we can provide the escape sequences in the string by following with forwarding slash (\), we can also provide the control character’s ASCII code in the range of hexadecimal values from 0x00 to 0x1f and 0x7f (Del).

To assign characters to value ASCII format (hexadecimal value), we use \x with the value.

Syntax:

    ctype_cntrl(string) : bool

Example:

    Input: "\r\n"
    Output: true
    Input: "\t\x12"
    Output: true
    Input: "\x00\x12\x1f\x7f"
    Output: true
    Input: "Hello123"
    Output: false

PHP code:

<?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");                
?>

Output

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.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.