[英]PHP preg_match matching value
My preg_match matches too much. 我的preg_match匹配太多。
SEARCH HTML: 搜索HTML:
$html = '<html><body>etc etc name="foo" value="123456ABCD_!#123456" bla bla</body></html>';
MY PATTERN 我的模式
preg_match('/name=\"foo\" value=\"(.*)\"/', $html, $matches);
MY RESULT 我的结果
array (
0 => 'name="foo" value="123456ABCD_!#123456"',
1 => '123456ABCD_!#123456" bla bla</body></html>',
)
Any help will be appreciated. 任何帮助将不胜感激。
You need non-greddy modifier 您需要非贪婪修饰符
$html = '<html><body>etc etc name="foo" value="123456ABCD_!#123456" bla bla</body></html>';
preg_match('/name=\"foo\" value=\"(.*?)\"/', $html, $matches);
print_r($matches);
.*
is greedy; .*
是贪婪的; it matches everything it can. 它与一切都匹配。
You can use it non-greedy ( .*?
) or use this: [^"]*
. 您可以使用非贪婪( .*?
)或使用以下命令: [^"]*
。
.*
is also matching the closing quote. .*
也与结束语匹配。 Replace it by ( [^\\"]*
) 替换为( [^\\"]*
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.