2015-06-16 14:07



I'm looking to get this value 'B00DS4KJR4' enclosed in tags from a url by using file_get_contents() function in PHP. However, I'm failing to write the correct regex to find the value from this html source code the page:

<span class="a-text-bold">ASIN:

Can you help me to write the correct regex to find that particular value on the page ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • douba1904 douba1904 6年前

    You can use a regular expression like the following also presented on 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>.

    $html ='<span class="a-text-bold">ASIN:
    if (preg_match('/<span\s[^><]*>\s*ASIN:\s*<\/span>\s*<span>\s*([^><]*)\s*<\/span>/i', $html, $m)) {
        $asin = $m[1];
        print $asin;
    点赞 评论 复制链接分享
  • dongnao1908 dongnao1908 6年前

    The first span doesn't match with the regex because it has class in it, the other ones which are written like <span>anything</span> will be found.

    点赞 评论 复制链接分享