Home >Backend Development >PHP Tutorial >How to Convert Plain Text URLs to Clickable Links in HTML While Preserving URLs Within Tags?
Preserving URLs Within HTML Tags while Converting Non-Tagged URLs
In HTML documents, it can be desirable to convert plain text URLs into clickable links while excluding URLs that are already contained within HTML tags. This can present challenges, as many common text replacement methods also inadvertently target tagged URLs.
Problem Statement
The following HTML text snippet illustrates the issue encountered:
<code class="html"><p>I need you help here.</p> <p>I want to turn this:</p> <pre class="brush:php;toolbar:false">sometext sometext http://www.somedomain.com/index.html sometext sometext
into:
sometext sometext <a href="http://somedoamai.com/index.html">www.somedomain.com/index.html</a> sometext sometext
However, the existing regex solution also targets URLs within img tags:
sometext sometext <img src="http//domain.com/image.jpg"> sometext sometext
Converting this accidentally produces:
sometext sometext <img src="<a href="http//domain.com/image.jpg">domain.com/image.jpg</a>"> sometext sometext**Solution** To effectively isolate and replace URLs that are not within HTML tags, we can leverage XPath and DOM manipulation. Using an XPath query, we can select text nodes containing URLs while excluding those that are descendants of anchor tags:
$texts = $xPath->query(
'/html/body//text()[ not(ancestor::a) and ( contains(.,"http://") or contains(.,"https://") or contains(.,"ftp://") )]'
);
Once these text nodes are identified, we can replace them with document fragments containing the appropriate anchor elements. This ensures that the URLs are converted without affecting the surrounding HTML structure:
foreach ($texts as $text) {
$fragment = $dom->createDocumentFragment(); $fragment->appendXML( preg_replace( "~((?:http|https|ftp)://(?:\S*?\.\S*?))(?=\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)~i", '<a href=""></a>', $text->data ) ); $text->parentNode->replaceChild($fragment, $text);
}
The above is the detailed content of How to Convert Plain Text URLs to Clickable Links in HTML While Preserving URLs Within Tags?. For more information, please follow other related articles on the PHP Chinese website!