regular expressions don't work in 8.1.13 and 8.1.14
Description
The following code:
$text = 'src="index.php?act=captcha&{$random}" width="145"'; preg_match_all("/{\s*$([a-z\d]+)\s*}/", $text, $matches); print_r($matches);
Resulted in this output:
Array ( [0] => Array ( ) [1] => Array ( ) )
But I expected this output instead:
must be found '$random' and 'random'
PHP Version
PHP 8.1.14 and PHP 8.1.13
Operating System
windows7
There are two mistakes in your code:
- $ means "end of line", it should be
\$ - by using double quotes instead of single quotes, you risk variable substitution
The following code works for me:
<?php
$text = 'src="index.php?act=captcha&{$random}" width="145"';
preg_match_all('/{\s*\$([a-z\d]+)\s*}/', $text, $matches);
print_r($matches);
?>
@awn-wgg you can test your snippet here https://3v4l.org/lgihC and see, that is has the same behaviour also in 8.0.* and 8.2.* (and 7.4.*), which is a good indicator that the problem is probably in the snippet (as @nielsdos explained) and not php bug.
I have a backslash before $ in my program. I don't understand how I could insert a row without it. The line looks like this -
preg_match_all("/\{\s*\$([a-z\d]+)\s*\}/", $text, $matches);
But the regex doesn't work in all versions of php. In this I was convinced in https://3v4l.org/lgihC. Replacing double quotes with single quotes helped. I knew it but didn't see it. Thanks for the hints.