duanbiaoshu2021 2017-06-06 02:31
浏览 31
已采纳

PHP在脚本标记之间获取值

how to get value between the script tag using php scrapping.

$homepage = file_get_contents('http://www.example.com/');

//$homepage
<script type="application/ld+json">
  //FETCH ME
/script>
  • 写回答

2条回答 默认 最新

  • drbe16008 2017-06-06 02:41
    关注

    This seems like the answer you need here, Get content between two strings PHP

    The second answer seems easiest:

    $out = file_get_contents('http://www.example.com/');
    $start = "<script ...>"; // replace ... with exact other text you are maching
    $end = "</script>";
    $startsAt = strpos($out, $start) + strlen($start);
    $endsAt = strpos($out, $end, $startsAt);
    $result = substr($out, $startsAt, $endsAt - $startsAt);
    

    with $result being the as it says result of the content between $start and $end

    For multiple instances, just delete the first occurance from $out and repeat:

    $out = str_replace($start.$result.$end,"",$out);
    

    The first occurance is removed, so you can retreive the second occurance. But sure, there may be a more simpler way to get all occurances, not just this.

    $startsAt = strpos($out, $start) + strlen($start);
    $endsAt = strpos($out, $end, $startsAt);
    $result = substr($out, $startsAt, $endsAt - $startsAt);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案