preg_replace - Only match when surrounded by spaces
Here's my existing code:
preg_replace("!((([a-z]{3,5}://))". "[-a-z0-9.]{2,}\.[a-z]{2,4}". "(:[0-9]+)?". "(/([^\s]*[^\s,.])?)?". "(\?([^\s]*[^\s,.])?)?)!i", "开发者_StackOverflow中文版<a target=\"_blank\" href=\"\\1\">\\1</a>", $s);
It takes a link and turns it into HTML.
The problem is that sometimes I get a URL that I don't want to turn into HTML. For example:
<img src="http://www.domain.com/img.png" />
so that would turn into:
<img src="<a target="_blank" href ...
What is the best way to prevent this? I think only taking links between a space might work well. How would I alter the preg_replace?
You could use word boundaries (\b
).
preg_replace("!\b((([a-z]{3,5}://))". "[-a-z0-9.]{2,}\.[a-z]{2,4}". "(:[0-9]+)?". "(/([^\s]*[^\s,.])?)?". "(\?([^\s]*[^\s,.])?)?)\b!i", "<a target=\"_blank\" href=\"\\1\">\\1</a>", $s);
Though my preferred technique would be to search in text nodes only so you don't ever have to deal with serialised HTML.
If you decide to parse the HTML, DOMDocument is quite handy.
精彩评论