Re: Using DOM textContent Property

From: Date: Wed, 03 Sep 2008 15:55:21 +0000
Subject: Re: Using DOM textContent Property
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-279407@lists.php.net to get a copy of this message
Tim Gustafson wrote:
Hello, I am writing a filter in PHP that takes some HTML as input and goes through the HTML and adjusts certain tag attributes as needed. So, for example, if <a> tag is missing the "title" attribute, this filter adds a title attribute to the <a> tag. I'm doing this all using PHP 5 and the DOM parsing library, and it's working really well. The one snafu I'm running in to is dealing with users who will just type an e-mail address into an HTML document without actually making it a link - so, they'll just put foo@bar.com rather than <a href="mailto:foo@bar.com">foo@bar.com</a>. I'd like for these incorrectly entered e-mail addresses to magically change into real clickable links, so I'd like my filter to be able to grab those plain text e-mail addresses and convert them to actual clickable links. I tried iterating through all the elements on a page using something like this: $Elements = $HTML->getElementsByTagName("*"); for ($X = 0; $X < $Elements->length; $X++) { ... SNIP ... }
I think you might be better off using regexp on the text *before* sending it through the DOM parser. Send the user's text through a function that searches for URLs and email addresses, creating proper links as they're found, then use the output from that to move on to your DOM stuff. That way, you need not create new nodes in your nodelist.

« previous php.general (#279407) next »