我有这么一段代码
<td align=right> 
<a href=http://www.sodu.org/>第一页</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=http://www.sodu.org/index_2.html>上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=http://www.sodu.org/index_4.html>下一页</a>代码里也有可能没有 “<a href=http://www.sodu.org/index_2.html>上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;”
这一段现在我只想取出 下一页的url地址这个正则这么写

解决方案 »

  1.   

    <?php
    $str = <<<HT
    <td align=right> 
    <a href=http://www.sodu.org/>第一页</a>&nbsp;&nbsp;& nbsp;&nbsp;<a href=http://www.sodu.org/index_2.html>上一页</a>&nbsp;& nbsp;&nbsp;&nbsp;<a href=http://www.sodu.org/index_4.html>下一页</a>
    HT;
    preg_match_all('/<a[^>]*href=([^>]*)>下一页<\/a>/is', $str, $matches);
    print_r($matches[1]);
    ?>
      

  2.   

    $s = <<<s
    <td align=right>  
    <a href=http://www.sodu.org/>第一页</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=http://www.sodu.org/index_2.html>上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=http://www.sodu.org/index_4.html>下一页</a>
    s;preg_match('/.*<a href=(.*)>下一页<\/a>.*/', $s, $arr);echo $arr[1];
      

  3.   


    preg_match('/\<a href=(.*)[/s]*\>下一页\<\/a\>/U', $s, $arr);
      

  4.   


    /\<a.+?href=[\'\"\s]*(.*)[\'\"\s]*\>下一页\<\/a\>/Ui
      

  5.   


    $str = <<<HT
    <td align=right> 
    <a href=http://www.sodu.org/>第一页</a>&nbsp;&nbsp;& nbsp;&nbsp;<a href=http://www.sodu.org/index_2.html>上一页</a>&nbsp;& nbsp;&nbsp;&nbsp;<a href=http://www.sodu.org/index_4.html>下一页</a>
    HT;
    preg_match_all('/\<a.+?href=[\'\"\s]*(.*)[\'\"\s]*\>下一页\<\/a\>/Ui', $str, $matches);
    print_r($matches[1]);
      

  6.   

    preg_match_all('~<a[^>]*?href=[\"|\']?(.*?)[\"|\']?[^>]*?>下一页</a>~is',$html,$data);
    var_dump($data);要注意编码。
    程序文件的编码和字符串的编码不一致也取不出来。因为里面有中文
      

  7.   

    呵呵
    我想了一下还是不用这种方法了,中间有中文不太好弄,如果编码错误就麻烦了我是将几个<a>链接里的地址都取出来,然后直接取最后的地址就出来了虽然麻烦点,但是保险呵呵,谢谢大家的帮忙了。结贴,给分了