dongyiyu3953 2014-10-09 04:03
浏览 41
已采纳

在php.net中的示例preg_match()中使用$ subject和substr($ subject,3)之间的区别是什么

I read how to use function preg_match in http://php.net/manual/en/function.preg-match.php, I don't know what is differerent between using $subject and substr($subject,3) in preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3) and preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE). Please help me understand and check below function that why does it return empty array?

<?php
    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,"http://www.1gom.us/ti-le-keo-malaysia.html");
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    $content = curl_exec($ch);
    curl_close($ch);
    $regex = '/<div class="tabbox" id="tabbox">(.*)<\/div>/';
    preg_match($regex, $content, $matches, PREG_OFFSET_CAPTURE, 3);
    $table = $matches[1];
    print_r($table);
?>
  • 写回答

2条回答 默认 最新

  • duanchuang6978 2014-10-09 04:19
    关注

    You will obtain the same result. But if you use substr, you will create a new string for nothing, when the last parameter of preg_match only ask to begin the search at a particular offset of the subject string.

    The reason you obtain an empty result is probably due to the fact that . can't match newlines by default. You can change this behavior with the s modifier:

    $regex = '~<div class="tabbox" id="tabbox">(.*?)</div>~s';
    

    (Note the use of a different modifier to not have to escape slashes. Note too the use of a non-greedy quantifier to stop at the first occurence of </div>)

    However, as noticed in comments, extracting informations from an html document is more easy with a combo DOMDocument/DOMXPath (depending of how looks your document and what you are trying to do).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改