1

How can I get the text value where <span>Your name:<span> with the PHP simple HTML DOM parser without children command?

<div class="adds-info">
    <span>price:<span>1000000000 </span></span>
    <span>txt1</span>
    <span>Your name:<span>aaa</span></span>
    <span>Phone:<span>02632402210</span></span>
</div>

PHP simple HTML DOM parser api

3
  • Yes this works in CSS, but i need this in php simple html dom . Commented Oct 10, 2015 at 11:58
  • do you mean? <span>price:<?php echo $value; ?></span> Commented Oct 10, 2015 at 12:32
  • Apparently, things like :first-child are not supported. You can do something like $simpledomobject->find(".adds-info span span", 1), would that help? To print the text, you'd do echo $simpledomobject->find(".adds-info span span", 1)->innertext; Commented Oct 10, 2015 at 12:33

2 Answers 2

3

Assuming the HTML in your post is in a variable $str, this is fairly straightforward:

// Parse the HTML string into a DOM object
$html = str_get_html($str);

// Find the second nested span inside the <div class="adds-info">, then get its parent
$span = $html->find('.adds-info span span', 1)->parent;

// Get the innerhtml of the parent span
$name = $span->innertext;

If the number of elements changes, you need to check each one for the text you're looking for...

// Get all spans inside the <div class="adds-info">
$spans = $html->find('.adds-info span');

// Loop through each span, looking for "Your name:"
foreach ($spans as $span) {
    if (strpos($span->innerhtml, "Your name:") !== FALSE) {
        $name = $span->innerhtml;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I repeated my question with a little change in my expression. i need to get text after [span=Your name] Span value must be equal to [span=Your name] . sorry for my english
I've updated my answer, but why do you not want to use the children function?
Not because the number of children varies, i need outertext of <span>Your name:<span> ( AAA )
I updated my answer with an example of looping through each child span to find the one containing the text "Your name:" and return that span's innerhtml.
1

Thank you defines, i try by this and works

$spans = '.adds-info span';
foreach($spans as $n){  
                        if (strpos($n->innertext, "Your name:") !== FALSE) {
                            $name = $n->innertext;
                            $name = str_replace('Your name:', '', $name);
                            echo "Name: " . $name  . "<hr>";
                        }
                    }

2 Comments

There's no way this code works. This has a foreach iterating over a string.
That isn't different. It's still iterating over a string, which isn't possible...

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.