I have the following pattern and subject
$pattern = "\[item\s*id=['\"](.*?)['\"]\s*\]";
$subject = "[item id=\"40\"]";
Basically, I want to get the id attribute from a string, [item id="30"].
And I’ve tested my pattern and subject on different preg_match and preg_match_all websites and they all return the correct matches.
However, the following code I wrote seems to always return NULL for matches.
preg_match_all($pattern, $subject, $matches);
var_dump($matches);
What’s wrong? How can I do this?
You’re missing delimiters:
So
$patternshould look like:It returns: