Home >Backend Development >PHP Tutorial >How to Convert Plain Text URLs to Clickable Links in HTML While Preserving URLs Within Tags?

How to Convert Plain Text URLs to Clickable Links in HTML While Preserving URLs Within Tags?

Barbara Streisand
Barbara StreisandOriginal
2024-11-01 17:06:02370browse

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=&quot;http://somedoamai.com/index.html&quot;>www.somedomain.com/index.html</a> sometext sometext

However, the existing regex solution also targets URLs within img tags:

sometext sometext <img src=&quot;http//domain.com/image.jpg&quot;> sometext sometext

Converting this accidentally produces:

sometext sometext <img src=&quot;<a href=&quot;http//domain.com/image.jpg&quot;>domain.com/image.jpg</a>&quot;> 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(.,&quot;http://&quot;) or
    contains(.,&quot;https://&quot;) or
    contains(.,&quot;ftp://&quot;) )]'

);

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(
        &quot;~((?:http|https|ftp)://(?:\S*?\.\S*?))(?=\s|\;|\)|\]|\[|\{|\}|,|\&quot;|'|:|\<|$|\.\s)~i&quot;,
        '<a href=&quot;&quot;></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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn