I've written a method to get rid of unneeded elements in fetched DOM nodes, I've contacted the author, but simple dom has not been active for two years so I doubt he will include it in the distribution. Here it is:
/**
* remove specified nodes from selected dom
*
* @param string $selector
* @param int|array (optional) possible values include:
* + positive integer - remove first denoted number of elements
* + negative integer - remove last denoted number of elements
* + array of ones and zeroes - remove the respective matches that equal to one
*
* eg.
* // will remove first two images found in node
* $dom->removeNodes('img',2);
*
* // will remove last two images found in node
* $dom->removeNodes('img',-2);
*
* // will remove all but the third images found in node
* $dom->removeNodes('img',array(1,1,0,1));
*
* [!!!] if there are more matches found than elements in array, the last array member will be used for processing
*
* eg.
* // will remove second and every following image
* $dom->removeNodes('img',array(0,1));
*
* // will remove only the second image
* $dom->removeNodes('img',array(0,1,0));
*
* @return simple_html_dom_node
*/
public function removeNodes($selector, $limit = NULL)
{
$elements = $this->find($selector);
if ( empty($elements) ) return $this;
if ( isset($limit) && is_int( $limit ) && $limit < 0 ) {
$limit = abs( $limit );
$elements = array_reverse( $elements );
}
foreach ( $elements as $element ) {
if ( isset($limit) ) {
if ( is_array( $limit ) ) {
$current = current( $limit );
if ( next( $limit ) === FALSE ) {
end( $limit );
}
if ( !$current ) {
continue;
}
} else {
if ( --$limit === -1 ) {
return $this;
}
}
}
$element->outertext = '';
}
return $this;
}
put it in simple_html_dom_node
class or one extending it. In the askers case you'd use it like this:
$storyMessageNodes = $story->find('span.UIStory_Message');
$storyMessage = $storyMessageNodes[0]->removeNodes('a')->plaintext