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条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失