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

Learn More

Basic string match using ereg() function

9 Runs 66 Views 6 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/**
 *  basic string match using ereg() function
 * 
 */
$testStrings = array('dodgeball', '123samdei', '#@doremi', '!sadasd', 'jimy');
//this patters means any string but not one in the list so in our case any character 
//that is not a letter or a digit
$testPattern = '([^a-zA-Z0-9])';
$matches = array();
echo '<center>';
foreach ($testStrings as $string) {
    //if ereg will match at least one character that is not a letter or a digit it will return true
    if (ereg($testPattern, $string, $matches))
        echo '<br /><div style="background-color:red;color:#fff;padding:10px;width:400px;font-size:16px">
        The string <b>' . $string . '</b> does not contains only letters and/or digits and does match 
        our pattern:"' . $testPattern . '"  </div><br />';
    else {
        echo '<br /><div style="background-color:green;color:#fff;padding:10px;width:400px;font-size:16px">
        The string <b>' . $string . '</b> does contains only letters and/or digits and does not match
        our pattern:"' . $testPattern . '" </div><br />';
    }
}
echo '</center>';
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).