用PHP替换锚文本(和正则表达式) [英] Replace anchor text with PHP (and regular expression)

查看:189
本文介绍了用PHP替换锚文本(和正则表达式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含很多链接的字符串,在将它们打印到屏幕上之前,我想对其进行调整:

I have a string that contains a lot of links and I would like to adjust them before they are printed to screen:

我有类似以下内容:

<a href="http://www.dont_replace_this.com">replace_this</a>

,并希望最终得到这样的结果

and would like to end up with something like this

<a href="http://www.dont_replace_this.com">replace this</a>

通常我只会使用类似的东西:

Normally I would just use something like:

echo str_replace("_"," ",$url); 

在这种情况下,我无法执行此操作,因为URL包含下划线,因此断开了我的链接,以为我可以使用正则表达式来解决此问题.

In in this case I can't do that as the URL contains underscores so it breaks my links, the thought was that I could use regular expression to get around this.

有什么想法吗?

推荐答案

这将涵盖大多数情况,但我建议您进行检查以确保没有遗漏或更改任何意外情况.

This will cover most cases, but I suggest you review to make sure that nothing unexpected was missed or changed.

 pattern = "/_(?=[^>]*<)/";
 preg_replace($pattern,"",$url);

这篇关于用PHP替换锚文本(和正则表达式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆