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

Barbara Streisand
Release: 2024-11-01 17:06:02
Original
290 people have browsed it

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:
Copy after login

$texts = $xPath->query(

'/html/body//text()[
    not(ancestor::a) and (
    contains(.,&quot;http://&quot;) or
    contains(.,&quot;https://&quot;) or
    contains(.,&quot;ftp://&quot;) )]'
Copy after login

);

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:
Copy after login

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);
Copy after login

}

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!