PHP:如何在这里使用preg_match()?

时间:2022-04-09 20:14:09

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:

如何通过preg_match()函数捕获id和错误?我使用过这段代码,但没有帮助:

$file = 'stdClass Object ( [id] => 10303 )';
preg_match('/\[id\] \=\> ([0-9]+)/', $file, $mfc);
var_dump($mfc);

2 个解决方案

#1


1  

For error you can use this!

对于错误,您可以使用此!

preg_match('/\[error\][\s]*\=\>[\s]*(.*?)\?/', $file, $mfc);
var_dump($mfc);

And for ID

并为ID

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

#2


1  

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.

你的正则表达式的主要变化是考虑到任何前所未有的空白字符。

#1


1  

For error you can use this!

对于错误,您可以使用此!

preg_match('/\[error\][\s]*\=\>[\s]*(.*?)\?/', $file, $mfc);
var_dump($mfc);

And for ID

并为ID

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

#2


1  

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.

你的正则表达式的主要变化是考虑到任何前所未有的空白字符。