1

I am trying to create something for php html dom to work with a element path pattern.

It looks as fallow. I can have different paths where I want to have some text out. like;

$elements = 'h1;span;';
$elements = 'div.test;h2;span';

I tried to create an function to handle these inserts but I am stuck on the part to set 'getElementsByTagName()' in the good order and to receive the value of the last element,

what I have done now;

    function convertName($html, $elements) {

        $elements = explode(';', $elements);
        $dom = new DOMDocument;
        $dom->loadHTML($html);
        $name = null;

        foreach ($elements as $element) :
            $name. = getElementsByTagName($element)->item(0)->;
            endforeach;

         $test = $dom->$name.'nodeValue';
         print_r($test); // receive value          
   }

I hope someone can give me some input or examples.

3
  • you want to familiarize with XPath or use a third party lib supporting Selector, e.g. phpQuery or Zend_Dom Commented Mar 7, 2012 at 19:44
  • I dont want to put an lib with a big class for this thing, I think there has to be a solution to reach this in a simple way :) Commented Mar 7, 2012 at 20:19
  • sorry, I just got it, thanks anyway! :) Commented Mar 7, 2012 at 20:36

1 Answer 1

1

May be something like this:

function convertName($html, $elements) {
   $doc = new DOMDocument();
   libxml_use_internal_errors(true);
   $doc->loadHTML($html); // loads your html
   $xpath = new DOMXPath($doc);

   $elements = explode(';', $elements);
   $elemValues = array();

   foreach ($elements as $element) {
      $nodelist = $xpath->query("//$element");
      for($i=0; $i < $nodelist->length; $i++)
         $elemValues[$element][] = $nodelist->item($i)->nodeValue;
   }
   return $elemValues;
}

// TESTING
$html = <<< EOF
 <span class="bar">Some normal Text</span>
 <input type="hidden" name="hf" value="123">
 <h1>Heading 1<span> span inside h1</span></h1>
 <div class='foo'>Some DIV</div>
 <span class="bold">Bold Text</span>
 <p/>
EOF;

$elements = 'h1;span;';
// replace all but last ; with / to get valid XPATH
$elements = preg_replace('#;(?=[^;]*;)#', '/', $elements);
// call our function
$elemValues = convertName($html, $elements);
print_r($elemValues);

OUTPUT:

Array
(
    [h1/span] => Array
        (
            [0] =>  span inside h1
        )

)
Sign up to request clarification or add additional context in comments.

2 Comments

Its almost like this but return is separated, this one will give all <span> results back found in the document. When the $elements is 'h1;span', it means I just want to take the nodeValue out of 'span' in the 'h1'.
Are you saying h1;span is your version of XPATH to the span inside h1? If that's the case above code easily be tweaked. Pls see my update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.