dso89762 2019-04-11 09:50
浏览 103
已采纳

正则表达式根据用户名从href属性中提取URL

I'm trying to extract all the links that comes from after a specific user, But my regex only grab one link:

HTML:

<div class="from_name">
   USERNAME
</div>
<div class="media_wrap clearfix">
   <div class="media clearfix pull_left media_photo">
      <div class="fill pull_left">
      </div>
      <div class="text">
         <a href="https://google.com</a>
      </div>
   </div>
   <div class="text">
      <a href="https://yahoo.com</a>
   </div>
</div>

Codes:

preg_match_all('/USERNAME[\s\S]*?href="(.*?)</', $data, $matches);

print_r($matches);

//output, it's only caputre google.com :

Array
(
    [0] => Array
        (
            [0] => FullCapture

    [1] => Array
        (
            [0] => https://google.com
        )

)
  • 写回答

2条回答 默认 最新

  • dougaodi8895 2019-04-11 15:30
    关注

    Can you use two regex? First to match the entire area with USERNAME and second to match the urls.

    preg_match('/(?<=USERNAME).*(?<=href=").*?</s', $string, $matches);
    
    preg_match_all('/(?<=href=").*?(?=<)/', $matches[0], $newMatches);
    
    var_dump($newMatches);
    

    This gives you:

    array(1) {
      [0]=>
      array(2) {
        [0]=>
        string(18) "https://google.com"
        [1]=>
        string(17) "https://yahoo.com"
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!