Using DOM textContent Property

From: Date: Tue, 02 Sep 2008 21:18:01 +0000
Subject: Using DOM textContent Property
Groups: php.general 
Request: Send a blank email to php-general+get-279349@lists.php.net to get a copy of this message
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 ... } And then I tried looking at the textContent property of each node, but it seems that higher-level nodes include all the text of their children nodes (which is what the DOM documents say it should). But there doesn't appear to be any way to know if the textContent you've got is for just one node, or for a whole bunch of nodes. Is there any way to figure that out, so that I can adjust the textContent property of just the lowest-level nodes, rather than mucking up the higher-level ones? Tim Gustafson SOE Webmaster UC Santa Cruz tjg@soe.ucsc.edu 831-459-5354

« previous php.general (#279349) next »