PHP – regex to allow letters and numbers only

1. Use PHP’s inbuilt ctype_alnum You dont need to use a regex for this, PHP has an inbuilt function ctype_alnum which will do this for you, and execute faster: <?php $strings = array(‘AbCd1zyZ9’, ‘foo!#$bar’); foreach ($strings as $testcase) { if (ctype_alnum($testcase)) { echo “The string $testcase consists of all letters or digits.\n”; } else { … Read more

Optional Whitespace Regex

Add a \s? if a space can be allowed. \s stands for white space ? says the preceding character may occur once or not occur. If more than one spaces are allowed and is optional, use \s*. * says preceding character can occur zero or more times. ‘#<a href\s?=”https://stackoverflow.com/questions/14293024/(.*?)” title\s?=”https://stackoverflow.com/questions/14293024/(.*?)”><img alt\s?=”https://stackoverflow.com/questions/14293024/(.*?)” src\s?=”https://stackoverflow.com/questions/14293024/(.*?)”[\s*]width\s?=”150″[\s*]height\s?=”https://stackoverflow.com/questions/14293024/(.*?)”></a>#’ allows an optional … Read more

preg_match(): Compilation failed: invalid range in character class at offset

The problem is really old, but there are some new developments related to PHP 7.3 and newer versions that need to be covered. PHP PCRE engine migrates to PCRE2, and the PCRElibrary version used in PHP 7.3 is 10.32, and that is where Backward Incompatible Changes originate from: Internal library API has changed The ‘S’ … Read more