高手帮忙看下

441829 2011-06-02 11:42:16
高手帮我看下这段程序怎么没有实现替代的效果
<?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>
...全文
102 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2011-06-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 441829 的回复:]
格式问题处在哪里啊
[/Quote]
是没有毛病!

$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>
对比一下差异,当然这无碍大局
螃蟹k3179 2011-06-02
  • 打赏
  • 举报
回复
转意符号之前要加反斜杠,比如 . \ / - 这些符号 ,还有就是链接里还包含很多其他的符号,比如 ? & % | 等等
441829 2011-06-02
  • 打赏
  • 举报
回复
我看来看去都没看出什么毛病呢
441829 2011-06-02
  • 打赏
  • 举报
回复
格式问题处在哪里啊
xuzuning 2011-06-02
  • 打赏
  • 举报
回复
$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>.

你本身的也是正确的,只不过格式上有点问题
螃蟹k3179 2011-06-02
  • 打赏
  • 举报
回复
$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);
www_7di_net 2011-06-02
  • 打赏
  • 举报
回复
换成preg_match()吧,ereg要被淘汰了

<?php
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>
441829 2011-06-02
  • 打赏
  • 举报
回复
而不是楼上那种结果
441829 2011-06-02
  • 打赏
  • 举报
回复
我吧上面的程序复制到我的编辑器里面,运行后,却是this is a link to http://www.wjgilmore.com/.的结果

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧