<?php
$url = 'http://www.qq.com/'; 
$info=file_get_contents($url);
preg_match('| <div id="nav" class="nav">(.*?)</div>|i',$info,$m);
echo substr_replace('<a href="*">', '<li><a target="_blank" href="http://www.qq.com/文字">', 1);
echo $m[1];
?>
小弟想 获取 www.qq.com 并且想将里面的 链接 文字 进行替换,并且显示出来,请问哪里搞错了。
思路是:  获取http://www.qq.com/
            获取到的内容为 <a href="www.222.cc/234234.html"> 厉害</a>替换为  <a href="www.123.com/tag?=你很厉害>

解决方案 »

  1.   

    $patterns = '/(<a href=")(.*?)(">)/';
    $string = '<a href="www.222.cc/234234.html">';
    $replacements = '${1}www.123.com/tag?=你很厉害${3}';
    var_dump(preg_replace($patterns, $replacements, $string));我随意写了个,你自己改进吧
      

  2.   


    <?php
    $url = 'http://www.qq.com/'; 
    $info=file_get_contents($url);
    echo str_replace('<a href="www.222.cc/234234.html"> 厉害</a>','<a href="www.123.com/tag?=你很厉害>你狠厉害</a>',$info);
    ?>
      

  3.   


    $url = 'http://www.qq.com/'; 
    $info = file_get_contents($url);
    $pattern = '/<a (.*?)href(\s*)=(\s*)[\'|"](.*?)[\'|"](.*?)>/';
    $replacement = '<a ${1}href="http://www.111111.com"${5}>';
    var_dump(preg_replace($pattern, $replacement, $info));