一个数组 内容是
<a href="view?v=image&p=%E9%A3%8E%E6%99%AF&b=0&x=w0&pid=ysearch" target="_blank" name="doc3_p"><img src="http://tn1.cn2.yahoo.com/image/15ba/02add37e762998965f.jpeg" onload="setImgSize(this,170,170);"></a>我只要得到 view?v=image&p=%E9%A3%8E%E6%99%AF&b=0&x=w0&pid=ysearch 正则应该怎么写

解决方案 »

  1.   

    应该有你想要的,不知对不对
    $str = '<a href="view?v=image&p=%E9%A3%8E%E6%99%AF&b=0&x=w0&pid=ysearch" target="_blank" name="doc3_p"><img src="http://tn1.cn2.yahoo.com/image/15ba/02add37e762998965f.jpeg" onload="setImgSize(this,170,170);"></a>';

    preg_match_all ('|^<a href=\"(.*)\".*|U',$str,$out, PREG_PATTERN_ORDER);
    print_r($out);Array
    (
        [0] => Array
            (
                [0] => <a href="view?v=image&p=%E9%A3%8E%E6%99%AF&b=0&x=w0&pid=ysearch"
            )    [1] => Array
            (
                [0] => view?v=image&p=%E9%A3%8E%E6%99%AF&b=0&x=w0&pid=ysearch
            ))
      

  2.   

    我是这样子写的 
    foreach($img as $img_url){
     
    }
    $img是包含有<a 等内容 我现在想要生成一个新的数组 内容就只有 href里面的内容
    也就是 view?v=image&p=%E9%A3%8E%E6%99%AF&b=0&x=w0&pid=ysearch
      

  3.   


    //纯手写,没测试
    preg_match_all('/href=[\'"]+(.*)[\'"]+/',$str,$matches);var_dump($matches[1]);
      

  4.   


    <?php
    $str = '
    <a href="view?v=image&p=%E9%A3%8E%E6%99%AF&b=0&x=w0&pid=ysearch" target="_blank" name="doc3_p">
    <img src="http://tn1.cn2.yahoo.com/image/15ba/02add37e762998965f.jpeg" onload="setImgSize(this,170,170);"></a>
    ';
    preg_match("/<a\s*href=\"(.*?)\"[^>]*>.*?<\/a>/is", $str, $aMatch);
    print_r($aMatch[1]);
    ?>
      

  5.   

    <?php
    $str = '<a href="view?v=image&p=%E9%A3%8E%E6%99%AF&b=0&x=w0&pid=ysearch" target="_blank" name="doc3_p"><img src="http://tn1.cn2.yahoo.com/image/15ba/02add37e762998965f.jpeg" onload="setImgSize(this,170,170);"></a>';preg_match_all('/href=(\'|"|\s)*([^\'">\s]+)/i',$str,$match);
    print_r($match);preg_match_all('/src=(\'|"|\s)*([^\'">\s]+)/i',$str,$match);
    print_r($match);
    ?>