PHP ctype_punct() function with example

PHP ctype_punct() work: Here, we will find out about the ctype_punct() work with example in PHP.

PHP ctype_punct() work

ctype_punct() work is a character type (CType) work in PHP, it is utilized to check whether a given string contains all accentuation characters (can be considered as uncommon characters) or not.

It returns genuine, if all characters of the given strings are accentuation characters, else it returns false.

Syntax:

    ctype_punct(string) : bool

Example:

    Input: "@!~#"
    Output: true
    Input: "@!  #~"
    Output: false
    Input: "Hello@!"
    Output: false

PHP Code:

<?php
    $str = "@!~#";
    if(ctype_punct($str))
        echo ("$str contains all punctuation characters.\n");
    else
        echo ("$str does not contain all punctuation characters.\n");

    $str = "@!  #~"; //space is there
    if(ctype_punct($str))
        echo ("$str contains all punctuation characters.\n");
    else
        echo ("$str does not contain all punctuation characters.\n");

    $str = "HELLO@!"; //alphabets are there
    if(ctype_punct($str))
        echo ("$str contains all punctuation characters.\n");
    else
        echo ("$str does not contain all punctuation characters.\n");

    $str = "@!123"; //digits are there
    if(ctype_punct($str))
        echo ("$str contains all punctuation characters.\n");
    else
        echo ("$str does not contain all punctuation characters.\n");
?>

Output

@!~# contains all punctuation characters.
@!  #~ does not contain all punctuation characters.
HELLO@! does not contain all punctuation characters.
@!123 does not contain all punctuation characters. 

Leave a Comment

error: Alert: Content is protected!!