Tech Kid
Tech Kid

Reputation: 577

PHP: How to use preg_match() here?

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

Answers (2)

Debra
Debra

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

cs95
cs95

Reputation: 402423

Id

preg_match('/\[id\][\s]*\=\>[\s]*([0-9]+)/', $file, $mfc);

Error Text

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

Related Questions