In the previous tutorial we have seen that how can we extract links images from a URL . now in this tutorial we will learn how to extract the plain text from a URL or div or any other tag

<!DOCTYPE html>
<html lang="en-US" prefix="og: http://ogp.me/ns#">
<head>
<meta charset="UTF-8">
</head>
<body>
<?php
include "simple_html_dom.php";
$url = "http://www.fabulouspost.com/2017/06/kaise-pata-lagaye-koi-aappar-fida-hai.html";
$html = file_get_html($url);
echo $html->plaintext;
?>
</body>
</html>

After running the code if everything goes right then you can see a plain text from the website in this way you can simply parse all the plain text from the website but what to do if we want to parse only text from any ‘tag’, ‘DIV’, ‘ID’, or any ‘class’, name then we have to use the following code

<!DOCTYPE html>
<html lang="en-US" prefix="og: http://ogp.me/ns#">
<head>
<meta charset="UTF-8">
</head>
<body>
<?php
include "simple_html_dom.php";
$url = "http://www.fabulouspost.com/2017/06/kaise-pata-lagaye-koi-aappar-fida-hai.html";
$html = file_get_html($url);
foreach($html->find('h3') as $element)
{
echo "<br>". $element->innertext ;
}
?>
</body>
</html>

The above code will let you extract all the H3 tags in the document for extracting any other tags simply replace H3 to tag name for example P H1 H2 H3 H4 h5 H6 etc …


For extracting text between the any “ID” or any “class” name simply replace your tag name with “#ID-name” or “.class-name” respectively for example

<!DOCTYPE html>
<html lang="en-US" prefix="og: http://ogp.me/ns#">
<head>
<meta charset="UTF-8">
</head>
<body>
<?php
include "simple_html_dom.php";
$url = "http://www.fabulouspost.com/2017/06/kaise-pata-lagaye-koi-aappar-fida-hai.html";
$html = file_get_html($url);
foreach($html->find('.post-title') as $element)
{
echo "<br>". $element->innertext ;
}
?>
</body>
</html>

The above code will extract the heading of a web page in the given URL in the same way if you use #div-name then the text inside that, ID will extracted

Now suppose that if you have more than one particular tag for example let’s say H3 tag and you want only one tag among of them then how can you retrieve the particular tag from them in this situation you can create an Array and then count the position of that particular text and then you can simply show that array… in this way


<!DOCTYPE html>
<html lang="en-US" prefix="og: http://ogp.me/ns#">
<head>
<meta charset="UTF-8">
</head>
<body>
<?php
include "simple_html_dom.php";
$url = "http://www.fabulouspost.com/2017/06/kaise-pata-lagaye-koi-aappar-fida-hai.html";
$html = file_get_html($url);
$a = array();
foreach($html->find('h3') as $element)
{
$a[] = $element->innertext ;
}
echo "<pre>";
print_r($a);//see the position and display whatever you want
echo "</pre>";
echo $a[1];
?>

</body>
</html>

In the above code  we have searched for H3 tag then we got 16 H3 tag in the whole document now all of 16  H3 tag we will show first text that is heading of the document as shown in the code ..