高手帮我看下这段程序怎么没有实现替代的效果
<?php
$text="this is a link to http://www.wjgilmore.com/.";
echo ereg_replace("http://([a-zA-Z0-9./-]+)$","<a href=\"\\0\">\\0</a>",$text);
?>
我的目的是显示带有<a http://www.wjgilmore.com>http://www.wjgilmore.com</a>

解决方案 »

  1.   

    换成preg_match()吧,ereg要被淘汰了<?php
    $subject = "abcdef";
    $pattern = '/^def/';
    preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
    print_r($matches);
    ?> 
      

  2.   

    $p =  "/http:\/\/[a-zA-Z0-9\&\?\%\#\.\-\/]+/";
    $r =   "<a href=\"\\0\">\\0</a>";$t =   "this is a link http://asdf.asdf.asdf/";echo preg_replace($p,$r,$t);
      

  3.   

    $text="this is a link to http://www.wjgilmore.com/.";
    echo ereg_replace("http://[a-zA-Z0-9./-]+[^/.]/","<a href=\"\\0\">\\0</a>",$text);this is a link to <a href="http://www.wjgilmore.com">http://www.wjgilmore.com</a>.你本身的也是正确的,只不过格式上有点问题
      

  4.   

    转意符号之前要加反斜杠,比如 . \ / - 这些符号 ,还有就是链接里还包含很多其他的符号,比如 ? & % | 等等
      

  5.   

    是没有毛病!$text="this is a link to http://www.wjgilmore.com/.";
    echo ereg_replace("http://([a-zA-Z0-9./-]+)$","<a href=\"\\0\">\\0</a>",$text);
    得到
    this is a link to <a href="http://www.wjgilmore.com/.">http://www.wjgilmore.com/.</a>
    对比一下差异,当然这无碍大局
      

  6.   

    我吧上面的程序复制到我的编辑器里面,运行后,却是this is a link to http://www.wjgilmore.com/.的结果