dsh102123 2015-06-16 14:07
浏览 71
已采纳

正则表达式模式在PHP中的file_get_contents中查找此特定字符串

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:
                    </span>
                    <span>B00DS4KJR4</span>

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

  • 写回答

2条回答 默认 最新

  • douba1904 2015-06-16 14:17
    关注

    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:
                    </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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?