Antidot PHP API  version 0.16.0
##PHP API to simplify integration of Antidot components
 All Classes Files Functions Variables Groups Pages
Static Public Member Functions | List of all members
DOMNodeHelper Class Reference

Helpers for DOM elements. More...

Static Public Member Functions

static get_text (DOMNode $node, array $callbacks=array())
 Retrieve text from children text nodes.

Detailed Description

Helpers for DOM elements.

Member Function Documentation

static get_text ( DOMNode  $node,
array  $callbacks = array() 
)
static

Retrieve text from children text nodes.

Parameters
$node[in] base node from which XML text nodes are looked for.
$callbacks[in] array of callbacks (default: empty array). Array keys should correspond to XML node types, array values are callbacks to be called for specific node names. These callbacks should accept a DOMNode as first parameter.
Returns
merged text from various text nodes.
Simple example:
Input XML document:
<root>
  <child_1>some content</child_1>
  <child_2>other content</child_2>
</root>
Let's suppose node variable points to DOMElement child_1. You can extract text content of child_1 node by calling:
assert(DOMNodeHelper::get_text($node) == 'some content');
Example with callback:
Input XML document:
<root>
  <child_1>some content</child_1>
  <child_2>other <match>specific</match> content</child_2>
</root>
Let's suppor node variable points to DOMElement child_2. You can extract text content of child_2 node and its child match by calling:
$filter = new FilterNode('match');
assert(DOMNodeHelper::get_text($node, array(XML_ELEMENT_NODE => $filter)) == 'other specific content');

If you want some formatting you can define you own FilterNode class or use provided one:

$filter = new BoldFilterNode('match');
assert(DOMNodeHelper::get_text($node, array(XML_ELEMENT_NODE => $filter)) == 'other <b>specific</b> content');

If you want different filters, each for different node name, you can provide array of filter instead of just one filter. For example:

$func_filter = new FilterNode('func');
$match_filter = new BoldFilterNode('match');
DOMNodeHelper::get_text($node, array(XML_ELEMENT_NODE => array($func_filter, $match_filter)));