比如 我获取到了百度的源码
<?php 
$url = "http://www.baidu.com"; 
$contents = file_get_contents($url); 
echo $contents; 
?> 
然后我想要的替换里面的连接
比如
href="http://baike.baidu.com" 把http://baike.baidu.com 替换成一个变量 然输出类似与 href="$newurl"

解决方案 »

  1.   

    匹配URL:
    [a-zA-Z]+://(\w+(-\w+)*)(\. (\w+(-\w+)*))*(\?\s*)?$
     或:
    http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/= \?%\-&_~`@[\]\':+!]*([<>\"\"])*$
      

  2.   

    $newurl 这个变量是从数据库里面随机读取的
    获源码 把原来的连接替换成 $newurl的值!
      

  3.   

    $newurl='sohu.com';
    echo preg_replace('/href="(.+?)"/i','href="'.$newurl.'"',$contents); 
      

  4.   

    $strText = preg_replace("/href[ ]*=[ ]*\"(.*?)\"/si", $myvalue, $strText);