1

I need to get all of the text contained between a specific div. In the following example I want to get everything between the div with class name "st" :

<div class="title">This is a title</div>
<div class="st">Some example <em>text</em> here.</div>
<div class="footer">Footer text</div>

So the result would be

Some example <em>text</em> here.

or even just

Some example text here.

Does anyone know how to accomplish this?

Jizbo Jonez
  • 1,230
  • 10
  • 25
  • 41

5 Answers5

3

Server-side in PHP

A very basic way would be something like this:

$data = ''; // your HTML data from the question
preg_match( '/<div class="\st\">(.*?)<\/div>/', $data, $match );

Then iterate the $match object. However, this could return bad data if your .st DIV has another DIV inside it.

A more proper way would be:

function getData()
{
    $dom = new DOMDocument;
    $dom -> loadHTML( $data );
    $divs = $dom -> getElementsByTagName('div');

    foreach ( $divs as $div )
    {
        if ( $div -> hasAttribute('class') && strpos( $div -> getAttribute('class'), 'st' ) !== false )
        {
            return $div -> nodeValue;
        }
    }
}

Client-side

If you're using jQuery, it would be easy like this:

$('.st').text();

or

$('.st').html();

If you're using plain JavaScript, it would be a little complicated cause you'll have to check all DIV elements until you find the one with your desired CSS class:

function foo()
{
    var divs = document.getElementsByTagName('div'), i;

    for (i in divs)
    {
        if (divs[i].className.indexOf('st') > -1)
        {
            return divs[i].innerHTML;
        }
    }
}
alizahid
  • 959
  • 6
  • 17
1

Use DOM. Example:

$html_str = "<html><body><div class='st'>Some example <em>text</em> here.</div></body></html>";
$dom = new DOMDocument('1.0', 'iso-8859-1');

$dom->loadHTML($html_str); // just one method of loading html.
$dom->loadHTMLFile("some_url_to_html_file");


$divs = getElementsByClassName($dom,"st");
$div = $divs[0];

$str = '';
foreach ($div->childNodes as $node) {
    $str .= $dom->saveHTML($node);
}

print_r($str);

The below function is not mine, but this user's. If you find this function useful, go to the previously linked answer and vote it up.

function getElementsByClassName(DOMDocument $domNode, $className) {
    $elements = $domNode->getElementsByTagName('*');
    $matches = array();
    foreach($elements as $element) {
        if (!$element->hasAttribute('class')) {
            continue;
        }
        $classes = preg_split('/\s+/', $element->getAttribute('class'));
        if (!in_array($className, $classes)) {
            continue;
        }
        $matches[] = $element;
    }
    return $matches;
}
Community
  • 1
  • 1
Daedalus
  • 7,586
  • 5
  • 36
  • 61
0

PHP is a server side language, to do this you should use a client side language like javascript (and possibly a library like jQuery for easy ad fast cross-browser coding). And then use javascript to send the data you need to the backend for processing (Ajax).

jQuery example:

var myText = jQuery(".st").text();

jQuery.ajax({
    type: 'POST',
    url: 'myBackendUrl',
    myTextParam: myText,
    success: function(){
        alert('done!');
    },
});

Then, in php:

<?php
    $text = $_POST['myTextParam'];
    // do something with text
Asciiom
  • 9,867
  • 7
  • 38
  • 57
0

Using a XML parser:

$htmlDom = simple_load_string($htmlSource);
$results = $htmlDom->xpath("//div[@class='st']/text()");

while(list( , $node) = each($result)) {
    echo $node, "\n";
}
Maks3w
  • 6,014
  • 6
  • 37
  • 42
-1

use jquery/ajax

then do something like:

<script>
$(document).ready(function() {
$.ajax({
        type: "POST",
        url: "urltothepageyouneed the info",
        data: { ajax: "ajax", divcontent:$(".st").html()}
        })

});
</script>

Basically

$(".st").html()

will return the HTML

and

 $(".st").text()

Will return the text

Hope that helps

Neta Meta
  • 4,001
  • 9
  • 42
  • 67