Jeffrey L. Roberts Jeffrey L. Roberts - 25 days ago 8
PHP Question

PHP Regex Match Whitespace

I am having trouble matching the space after

in the following regex

$metatag = '<meta name="generator" content="WordPress 4.8.2">';
$metaregex = '/<meta.*?content="Wordpress.(?<version>.*?)"/';

preg_match($metaregex, $metatag, $matches);


Answer Source

It's a typo mistake. Instead of:

$metaregex = '/<meta.*?content="Wordpress.(?<version>.*?)"/';

It should be:

$metaregex = '/<meta.*?content="WordPress.(?<version>.*?)"/';

Notice Wordpress vs WordPress.

Or you can ignore case by using the i modifier:

$metaregex = '/<meta.*?content="wordpress.(?<version>.*?)"/i';
  • i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])