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

Learn More

Check if a string contains only alphabetic characters with ctype_alpha

92 Runs 1296 Views 191 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/**
 * Example for ctype_alpha() function usage  
 * checks if a string contains only alphabetic 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('PQWsde23', 'mom!#dad%', 'caciiiinnng');
foreach ($exampleStrings as $string) {
    if (ctype_alpha($string)) {
        //if the $string contains only alphabetic charcaters display the message bellow
        echo '<br /><div style="background-color:green;color:#fff;padding:10px;width:400px;font-size:16px">
        The string <b>' . $string . '</b> consists of all alphabetic characters</div><br />';
    } else {
        //if the $string does not contains only alphabetic charcaters display the message bellow
        echo '<br /><div style="background-color:red;color:#fff;padding:10px;width:400px;font-size:16px">
        The string <b>' . $string . '</b> does not consist of all alphabetic 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).