Skip to content
Thomas Weinert edited this page Jul 12, 2018 · 2 revisions

FluentDOM\Query::parents()

FluentDOM\Query parents([ $selector = NULL ])

Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector.

Usage

$html = <<<HTML
<html>
  <head>
    <title>Examples: FluentDOM\Query::parents()</title>
  </head>
  <body>
    <div>
      <p>
        <span>
          <b>My parents are: </b>
        </span>
      </p>
    </div>
  </body>
</html>
HTML;

$fdQuery = FluentDOM($html);
$parents = implode(
  ', ',
  $fdQuery
    ->find('//b')
    ->parents()
    ->map(
        create_function('$node', 'return $node->tagName;')
      )
);
echo $fdQuery
  ->find('//b')
  ->append('<strong>'.htmlspecialchars($parents).'</strong>');

Output

<?xml version="1.0"?>
<html>
  <head>
    <title>Examples: FluentDOM\Query::parents()</title>
  </head>
  <body>
    <div>
      <p>
        <span>
          <b>My parents are: <strong>span, p, div, body, html</strong></b>
        </span>
      </p>
    </div>
  </body>
</html>
Clone this wiki locally