[英]preg_match_all() html tags
Using preg_match_all(), I want to match something like: 使用preg_match_all(),我想匹配以下内容:
...randomtext...>MATCH1</a>" (MATCH2)"...randomtext...
EDIT: to clarify, this is exactly the string I'm trying to extract data from, including the brackets, quotes, angle-brackets etc. ...randomtext...>MATCH1</a>" (MATCH2)"...randomtext...
编辑:澄清一下,这正是我要从中提取数据的字符串,包括方括号,引号,尖括号等
Here's what I've tried: preg_match_all("/^>(.+?)</a>\\" \\((.+?)\\)\\"$/", $htmlfile, $matches);
这是我尝试过的方法: preg_match_all("/^>(.+?)</a>\\" \\((.+?)\\)\\"$/", $htmlfile, $matches);
It should extract MATCH1
as $matches[1][0]
and MATCH2
as $matches[2][0]
它应将MATCH1
提取为$matches[1][0]
,将MATCH2
为$matches[2][0]
Any idea why it isn't working? 知道为什么它不起作用吗?
Thanks 谢谢
You need to escape the /
in your pattern, and you don't want your pattern anchored to ^
and $
您需要在模式中转义/
,并且不希望将模式锚定到^
和$
So probably this will work: preg_match_all("/>(.+?)<\\/a>\\" \\((.+?)\\)\\"/", $htmlfile, $matches);
因此,这可能会起作用: preg_match_all("/>(.+?)<\\/a>\\" \\((.+?)\\)\\"/", $htmlfile, $matches);
You didn't escape your end tag </a>
您没有逃脱结束标签</a>
This should work: 这应该工作:
preg_match_all("/>(.+?)<\/a>\" \((.*?)\)/", $htmlfile, $matches);
See Codepad example . 请参阅键盘示例 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.