0

how to get div class value using dom document

i need to echo this value 4,458 members

from the below code

< div class="mbs fcg">4,458 members< /div>

right now my orginal code is

$links = $dom->getElementsByTagName('div');

foreach ($links as $link){

echo $link->nodeValue;
echo $link->getAttribute('class');

}

how to target this particular class = mbc fcg ?

now with my present code i am getting all div values.

what changes i should do

4
  • 3
    possible duplicate of PHP XML/HTML DOM get CSS class attribute with whitespace Commented Jul 19, 2015 at 6:09
  • It is unclear what you want: 1) you ask to target a class value. 2) You want to echo a text-node ... So, what do you actually want? Commented Jul 19, 2015 at 6:18
  • sir, what i want is <div class="mbs fcg">4,458 members< /div> in this code i want to scrape this value 4,458 members Commented Jul 19, 2015 at 6:23
  • Duplicate stackoverflow.com/questions/4835300/… Commented Jul 19, 2015 at 7:10

3 Answers 3

2

you will need to use DOMXPath, which will take a DOMDocument instance

$xpath   = new DOMXPath( $dom );
// if the className doesn`t changes
$members = $xpath->query( '//div[@class="mbs fcg"]' );
// if the class name changes ex. class="mbs fcg my-other class-name"
$members = $xpath->query( '//div[contains(@class,"mbs fcg")]' );

alternatively if you want to iterate all over your div`s you could try

$divs = $dom->getElementsByTagName( 'div' );
foreach( $divs as $div ){
    // if the className doesn`t changes
    if( $div->getAttribute( 'class' ) === 'mbs fcg' ){
        echo $div->nodeValue;
    }
    // if the class name changes ex. class="mbs fcg my-other class-name"
    if( strpos( $div->getAttribute( 'class' ), 'mbs fcg' ) !== false ){
        echo $div->nodeValue;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

your welcome, i'm glad i could help you out. you should also mark it as right answer
1

NOTICE::: THIS IS A JAVASCRIPT SOLUTION ... NOT A PHP DOMDOCUMENT SOLUTION

Try this HTML:

<div id="ME" class="mbs fcg">4,458 members</div>

... and this Javascript:

var WANTED_TEXT = document.getElementById('ME').firstChild.nodeValue;

EDIT2:

If you actually want, to get all textnodes from all occurrences of elements having class='mbs cfg' ... then try the following HTML:

<div class="mbs fcg">4,458 members</div>

... and this Javascript:

var Collection = document.getElementsByClassName('mbs fcg');

for(i=0; i<Collection.length; i++) {
    Texts = Collection[i].firstChild.nodeValue;
    document.write('<p>'+Texts+'</p>');
}

That should echo the pure text from all elements in the Collection.

6 Comments

i dont have option to add div id sir. cos i am scrapping from other website
without using javascript can we scrape sir, i mean with the help of domdocument i want to scrap
I havn't tried myself ... But, I would think so, yes.
Ohhh ... this is PHP only ... sorry ... I didn’t notice that! ... You should disregard my answer, which is a JavaScript solution.
its ok sir, do u have any id how we can write in php
|
0

I think you need to use an id to target a single div, such as:

< div id="my_id_name" class="mbs fcg">4,458 members< /div>

1 Comment

sorry i cannot add div id, i need to scrape the data from other website. i only have to scrape <div class="mbs fcg">

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.