Possible Duplicate:
find and edit comment element of html with php可能重复:用php查找和编辑html的注释元素
Hello. Regular expressions in general are way over my head. The answer to my question should be fairly simple, but after digging around for a while I'm still at loss for one. How can I extract "Orange County, CA" using preg_match() in PHP from this HTML excerpt?:
你好。一般的正则表达式都是我的头脑。我的问题的答案应该相当简单,但经过一段时间的挖掘后,我仍然处于亏损状态。如何从这个HTML摘录中使用PHP中的preg_match()提取“Orange County,CA”?
<!-- CLTAG GeographicArea=Orange County, CA -->
Note that the text in this area will be variable and thus the regular expression needs to be dynamic to account for this.
请注意,此区域中的文本将是可变的,因此正则表达式需要是动态的以解决此问题。
2 个解决方案
#1
3
One regex you can use would be this:
你可以使用的一个正则表达式是这样的:
$matches = array();
preg_match('/<!-- CLTAG GeographicArea=(.*?) -->/', $html, $matches);
echo $matches[1];
If there are multiple such comments in the HTML, use preg_match_all()
.
如果HTML中有多个这样的注释,请使用preg_match_all()。
#2
0
regular expression:
正则表达式:
preg_match('#\<\-\-(.*)\=(.*)\-\-\>#', $html, $matches);
$matches[1] //contain data
#1
3
One regex you can use would be this:
你可以使用的一个正则表达式是这样的:
$matches = array();
preg_match('/<!-- CLTAG GeographicArea=(.*?) -->/', $html, $matches);
echo $matches[1];
If there are multiple such comments in the HTML, use preg_match_all()
.
如果HTML中有多个这样的注释,请使用preg_match_all()。
#2
0
regular expression:
正则表达式:
preg_match('#\<\-\-(.*)\=(.*)\-\-\>#', $html, $matches);
$matches[1] //contain data