doudou8081 2010-02-10 00:43
浏览 43
已采纳

Preg_match - 检索href

I have got stuck with a question I have just been helped on - its a new problem but only just slightly.

I have this preg_match to get the contents of href. Please don't tell me not to use regex - I am aware of using other parsers/classes etc but this is an old script that just needs to be fixed for now. :) No time for re-writes!

preg_match("~<a target=\'_blank\' rel=\'nofollow\' href=\"(.*?)\">~i", $epilink, $epiurl);

It returns:

http://www.example.com/frame2.php?view=&epi=54673-r

However, it should return:

http://www.example.com/frame2.php?view=168204&epi=54673

This is an example of html it would work with:

<a target='_blank' rel='nofollow' href="http://www.example.com/frame2.php?view=545903&epi=54683">

Why is the URL I have returned malformed?

Thanks all for any help.

  • 写回答

2条回答 默认 最新

  • dongyuli0964 2010-02-10 00:50
    关注
    $string="<a target='_blank' rel='nofollow' href=\"http://www.example.com/frame2.php?view=545903&epi=54683\">";
    $s = explode('">',$string);
    foreach($s as $k){
       if (strpos($k,"href")!==FALSE){
            echo preg_replace('/.*href="|/ms',"",$k);
            break;
       }
    }
    

    output

    $ php test.php
    http://www.example.com/frame2.php?view=545903&epi=54683
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services