Home » PHP » PHP Ctype functions

PHP ctype_graph() function with example

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

PHP ctype_graph() function

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

It returns true if all characters of the given strings are printable characters except whitespace. Else it returns false.

Printable characters are the characters that can be printed on the screen. The all alphabets, digits, whitespace and special characters are the printable characters. But, this function does not consider space as a printable character.

Syntax:

    ctype_graph(string) : bool

Example:

    Input: "Hello123"
    Output: true
    Input: "[email protected]#!~*.()"
    Output: true
    Input: "Hello world!"
    Output: false
    Input: "Hello\r\nWorld"
    Output: false
    Input: "Hello\x41"
    Output: true

PHP code:

<?php
    $str = "Hello123";
    if(ctype_graph($str))
        echo ("$str contains all printable characters.\n");
    else
        echo ("$str does not contain all printable characters.\n");

    $str = "[email protected]#!~*.()";
    if(ctype_graph($str))
        echo ("$str contains all printable characters.\n");
    else
        echo ("$str does not contain all printable characters.\n");

    $str = "Hello World";
    if(ctype_graph($str))
        echo ("$str contains all printable characters.\n");
    else
        echo ("$str does not contain all printable characters.\n");
	
    $str = "Hello\r\nWorld"; //"\r" and "\n" are not printable characters
    if(ctype_graph($str))
        echo ("$str contains all printable characters.\n");
    else
        echo ("$str does not contain all printable characters.\n");

    $str = "Hello\x41"; //"\x41" is the ASCII value of "A"
    if(ctype_graph($str))
        echo ("$str contains all printable characters.\n");
    else
        echo ("$str does not contain all printable characters.\n");	
?>

Output

Hello123 contains all printable characters.
[email protected]#!~*.() contains all printable characters.
Hello World does not contain all printable characters.
Hello
World does not contain all printable characters.
HelloA contains all printable 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.