1

I have following html code

<div id="b_changetext" class="FL gL_13 PT15"> <span class="gr_15 uparw_pc"><strong>5.80</strong></span> (+2.28%)</div>

I wanted to extract content (+2.28%) Tried following code

foreach($html->find('div[n_changetext]') as $e){
      echo $e->innertext . '<br>';
    echo "wwwwww";
}

On running it does not enter the for loop . ( "wwwwww" is not displayed)

Can anyone please suggest a solution

1
  • find('div#n_changetext') you can use dash for id selector and dot for a class Commented Jan 6, 2021 at 21:13

1 Answer 1

1

div[n_changetext] finds elements with an n_changetext attribute (which is not valid in HTML).

To find an element with a given id you must specify that the name of the attribute is id and specify the value.

The value, in your example, starts with a b not an n:

find('div[id=b_changetext]')
Sign up to request clarification or add additional context in comments.

6 Comments

I have changed to find('div[id=b_changetext]') as you suggested but still it does not work. It does not enter the loop.
@kiranshiv — i.imgur.com/12NGsHH.png — Your code (with my modification) works fine when I test it.
<?php include('simple_html_dom.php'); $html = str_get_html('moneycontrol.com/india/stockpricequote/…); foreach($html->find('div[id=n_changetext]') as $e){ echo $e->innertext . '<br>'; echo "wwwwww"; } echo "ssssssssssss"; ?>
str_get_html expects to be passed a string of HTML, not a URL.
There's an example on the homepage of the PHP Simple HTML DOM Parser site!
|

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.