本文介绍了将文本转换为链接 - php 正则表达式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将纯文本转换为 url 时遇到了一些问题.我喜欢的是,如果我有这样的文字:www.google.com,它会被转换为

I'm having a bit of a problem with converting plain text to an url.What I like to have is, if I have text like this: www.google.com, it's converted to

<a href="www.google.com" target="_blank">www.google.com</a>

我是一个 RegEx 菜鸟,但我试过了:

I'm kind of a RegEx noob, but I tried this:

$description = preg_replace('@(www.([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $description);

描述变量是一段文本,其中可以包含未转换的网址.

The description var is a piece of text, which CAN contain unconverted url's.

通过上面的代码,我得到了这个链接:

With the code above, I get this as link:

<a target="_blank">www.google.com</a>

所以href部分被省略了.这对您那里的 RegEx 向导来说一定是小菜一碟,所以在此先感谢您的每一个帮助.

So the href part is left out. This must be a piece of cake for you RegEx wizards out there, so thanks in advance for every help.

如果有另一种(更好的?)将纯文本转换为 url 的方法,您可以这么说,我会尝试一下.

If there is another (better?) way to convert plain text to url's, you can say so and I'll try it.

推荐答案

我找到了解决方案.它确实与 RegEx 没有任何关系,这是正确的.我的同事在头部添加了这行 jquery 代码:

I've found the solution. It indeed didn't have anything to do with the RegEx, that was correct. My coworker added this line of jquery code in the head:

$("a").removeAttr('href');

很明显,href 属性被删除了.我没有看这个,因为我确定这是一个 php/regex 问题.删除它解决了问题.

So obviously the href attribute was being removed. I didn't look at this because I was sure this was a php/regex problem. Removing this fixed the problem.

我意识到这是一个愚蠢的错误,你们不可能解决这个问题,所以感谢大家的帮助,为你们 +1.

I realize this was a stupid error and it was impossible for you to solve this, so thanks all for helping, +1 to you guys.

这篇关于将文本转换为链接 - php 正则表达式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 06:58