$var='Hi Welcome'; // All are printable character(s) except space
$var='Hi \n welcome'; // All or some are NOT printable character(s)
if(ctype_print($var)){
echo "<b>$var</b> All are printable character(s) except space ";
}else{
echo " <b>$var</b> All or some are NOT printable character(s)";
}
Syntax
bool ctype_print ( string $input_string )
$input_string : String to be checked. $input_string are printable characters including space or not. Any thing other than this if present then it returns FALSE.
$str = array('string1' => "12$s", 'string2' => "Hello World", 'string3' => "asdf\n\r\t",'string4' => 43);
foreach ($str as $val => $string) {
if (ctype_print($string)) {
echo "<p class='text-success'>The string <b>$val</b> consists of all printable character(s)</p><br>";
}else {
echo "<p class='text-danger'>The string <b>$val</b> does not consist of all printable character(s)</p><br>";
}
}
Output is here
The string string1 consists of all printable character(s)
The string string2 consists of all printable character(s)
The string string3 does not consist of all printable character(s)
The string string4 does not consist of all printable character(s)
Why the $string4 => 43 is not printable chars in above code ?
$str="Hello World";
echo "output of ctype_print(): ".ctype_print($str);
echo "<br>";
echo "output of ctype_graph(): ".ctype_graph($str);
Output is here
output of ctype_print(): 1
output of ctype_graph():
Check for only alphabetic characters by ctype_alpha()
Check for all lower or upper
Check for at least one lower case or upper case
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.