[英]regex pattern to find this particular string in file_get_contents in PHP
I'm looking to get this value 'B00DS4KJR4' enclosed in tags from a url by using file_get_contents() function in PHP. 我正在寻找通过在PHP中使用file_get_contents()函数从URL中获取包含在标签中的值'B00DS4KJR4'。 However, I'm failing to write the correct regex to find the value from this html source code the page:
但是,我无法编写正确的正则表达式来从此html源代码页面中找到值:
<span class="a-text-bold">ASIN:
</span>
<span>B00DS4KJR4</span>
Can you help me to write the correct regex to find that particular value on the page ? 您能帮我写正确的正则表达式来在页面上找到该特定值吗?
You can use a regular expression like the following also presented on Regex101 . 您可以使用正则表达式,也可以在Regex101上使用以下表达式。 This looks for a
<span>
with any attributes, containing the string ASIN:
in the innerHTML followed by another <span>
and captures the contents of the second <span>
. 这将查找具有任何属性的
<span>
,在innerHTML中包含字符串ASIN:
:,后跟另一个<span>
并捕获第二个<span>
的内容。
$html ='<span class="a-text-bold">ASIN:
</span>
<span>B00DS4KJR4</span>';
if (preg_match('/<span\s[^><]*>\s*ASIN:\s*<\/span>\s*<span>\s*([^><]*)\s*<\/span>/i', $html, $m)) {
$asin = $m[1];
print $asin;
}
preg_match_all('/<span>(.*)<\/span>/',$the_html,$the_result_array);
第一个span
与正则表达式不匹配,因为它具有类,其他的则写成<span>anything</span>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.