[nycphp-talk] Regex for P Elements
Chris Snyder
chsnyder at gmail.com
Wed Jan 12 13:06:35 EST 2011
On Wed, Jan 12, 2011 at 9:30 AM, Jim Yi <jim at jimyi.com> wrote:
> This problem is much better suited for an XML parser, and it makes your code
> more readable as well. Code snippet:
> $dom = new DOMDocument();
> $dom->loadHTML($txt);
> $items = $dom->getElementsByTagName('p');
> foreach ($items as $paragraph) {
> echo $paragraph->nodeValue;
> // You can also manipulate and update the nodeValue within this loop as
> well
> }
> Jim Yi
>
As Rob Marscher pointed out, using the DOM support can be tricky. What
you get out is not always what you put in, even if what you put in was
technically well-formed. Dreamweaver-y code (empty <font></font>
garbage) is a nightmare.
It's also slower than regex (which is slower than strpos()). So yeah,
it's super convenient and clean, but there are big trade-offs to using
it in some cases.
More information about the talk
mailing list