Speed up development with full-stack environments for every branch.

Learn More

Check if a string contains only control characters with ctype_cntrl

12 Runs 84 Views 6 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/**
 * Example for ctype_cntrl() function usage  
 * checks if a string contains only control characters
 */
//$exampleStrings is an array with different strings that needs to be tested
echo '<!DOCTYPE html>
<html>
    <head>
        <title>example</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
     <body>
        <center>';
$exampleStrings = array('sea' => "sea", '\r\n\t' => "\r\n\t", 'dodge#4a' => "dodge#4a");
foreach ($exampleStrings as $stringName => $string) {
    if (ctype_cntrl($string)) {
        //if the $string contains only control charcaters display the message bellow
        echo '<br /><div style="background-color:green;color:#fff;padding:10px;width:400px;font-size:16px">
        The string <b>' . $stringName . '</b> consists of all control characters</div><br />';
    } else {
        //if the $string does not contains only control charcaters display the message bellow
        echo '<br /><div style="background-color:red;color:#fff;padding:10px;width:400px;font-size:16px">
        The string <b>' . $stringName . '</b> does not consist of all control characters</div><br />';
    }
}
echo '</center>
    <body>
</html>';
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).