Reputation: 577
I have two strings like
stdClass Object ( [id] => 10303 )
and
stdClass Object ( [error] => not found )
How do I catch id
and error
via preg_match()
the function? I've used this code but it didn't help:
$file = 'stdClass Object ( [id] => 10303 )';
preg_match('/\[id\] \=\> ([0-9]+)/', $file, $mfc);
var_dump($mfc);
Upvotes: 0
Views: 637
Reputation: 93
For error
you can use this!
preg_match('/\[error\][\s]*\=\>[\s]*(.*?)\?/', $file, $mfc);
var_dump($mfc);
And for ID
$syntax = '/\[id\][\s]*\=\>[\s]*([0-9]+)/';
preg_match($syntax, $file, $mfc);
var_dump($mfc);
Upvotes: 1
Reputation: 402423
preg_match('/\[id\][\s]*\=\>[\s]*([0-9]+)/', $file, $mfc);
preg_match('/\[error\][\s]*\=\>[\s]*(.*?)\?/', $file, $mfc);
The main change to your regex is to take into account any unprecedented whitespace characters present.
Upvotes: 1