tags:

views:

42

answers:

1
  $test_string = '<a href="#">blah blah</a>';
  $test_obj = simplexml_load_string($test_string);
  fb((string)$test_obj[0]);

I thought I could just access the contents of the anchor tag by doing $test_obj[0]. I'm doing this to move away from using regular expressions to access individual html elements and properties in PHP. Is this cast to string necessary or is there a better way?

thanks!

+1  A: 

If you are moving away from Regex to access DOM elements try:

PHP Simple HTML DOM Parser http://simplehtmldom.sourceforge.net/

I took me away from a whole world of pain I was suffering with Regex, then with SimpleXML and XPath. Hope this helps.

esryl
Suggested third party alternatives to [SimpleHtmlDom](http://simplehtmldom.sourceforge.net/) that actually use [DOM](http://php.net/manual/en/book.dom.php) instead of String Parsing: [phpQuery](http://code.google.com/p/phpquery/), [Zend_Dom](http://framework.zend.com/manual/en/zend.dom.html), [QueryPath](http://querypath.org/) and [FluentDom](http://www.fluentdom.org).
Gordon
@Gordon, very nice resources. thanks.
esryl
yes, thanks for those links. They look v. useful.
codecowboy