PHP:这里如何使用preg_match()?
PHP: How to use preg_match() here?
我有两个字符串
stdClass Object ( [id] => 10303 )
和
stdClass Object ( [error] => not found )
如何通过 preg_match()
函数捕获 id
和 error
?我已经使用了这段代码,但它没有帮助:
$file = 'stdClass Object ( [id] => 10303 )';
preg_match('/\[id\] \=\> ([0-9]+)/', $file, $mfc);
var_dump($mfc);
Id
preg_match('/\[id\][\s]*\=\>[\s]*([0-9]+)/', $file, $mfc);
错误文本
preg_match('/\[error\][\s]*\=\>[\s]*(.*?)\?/', $file, $mfc);
对您的正则表达式的主要更改是考虑出现的任何前所未有的空白字符。
对于 error
你可以使用这个!
preg_match('/\[error\][\s]*\=\>[\s]*(.*?)\?/', $file, $mfc);
var_dump($mfc);
对于ID
$syntax = '/\[id\][\s]*\=\>[\s]*([0-9]+)/';
preg_match($syntax, $file, $mfc);
var_dump($mfc);
我有两个字符串
stdClass Object ( [id] => 10303 )
和
stdClass Object ( [error] => not found )
如何通过 preg_match()
函数捕获 id
和 error
?我已经使用了这段代码,但它没有帮助:
$file = 'stdClass Object ( [id] => 10303 )';
preg_match('/\[id\] \=\> ([0-9]+)/', $file, $mfc);
var_dump($mfc);
Id
preg_match('/\[id\][\s]*\=\>[\s]*([0-9]+)/', $file, $mfc);
错误文本
preg_match('/\[error\][\s]*\=\>[\s]*(.*?)\?/', $file, $mfc);
对您的正则表达式的主要更改是考虑出现的任何前所未有的空白字符。
对于 error
你可以使用这个!
preg_match('/\[error\][\s]*\=\>[\s]*(.*?)\?/', $file, $mfc);
var_dump($mfc);
对于ID
$syntax = '/\[id\][\s]*\=\>[\s]*([0-9]+)/';
preg_match($syntax, $file, $mfc);
var_dump($mfc);