Saturday, April 16, 2011

How to display a selected part of HTML file using file_get_contents()

The PHP's file_get_contents() function can read a entire HTML page into a string.And we can use this function to get a page from internet and to display only the specified area we want,for example : 
if a file http://any_page.html has following design elements 

<html>
<body>
<div class='main'>
some content
</div>
<div class='extra'>
some unneeded content
</div>
</body>

</html>


and if we want to display only <div class='main'>some content</div> part in our webpage then we need to find out position of string <div class='main'> as the start point and position of string <div class='extra'> as a end point.To do that we need 3 more functions of PHP that are strpos()  strlen() and substr().

substr() : Returns the position of the first occurrence of matched string.
(note: there is a difference between strpos() and strrpos() ) 
strpos() : Returns the portion of string with "strat point" and "length" as parameters.
strlen() : Returns the length of the given string.


now with the knowledge of this functions you can use some function like this


<?php
function get_selected_area($strat_string,$end_string,$page_address) 
{
$whole_html = file_get_contents($page_address);
$strat_point = strpos($whole_html,$strat_string) ;
$end_point = strpos($whole_html,$end_string);
$length_of_needed_string = ($end_point - $strat_point) - 1; //because we don't want "<" in the end
$needed_part_of_html = substr($whole_html,$strat_point,$length_of_needed_string); 
return  $needed_part_of_html;
}
?>
Now if you put this function in a separate php file and use it via include() or require() the following function can be used any where in any page like
<?php  
echo get_selected_area("<div class='main'>","<div class='extra'>","http://any_page.html");
?>
 NOTE: The string chosen for strat and end must satisfy the HTML standards,for example :
 you can't do something like
<?php  
echo get_selected_area("<div class='main'>","</div>","http://any_page.html");
?>
as a HTML page may have several </div> even before the <div class='main'> and this will surely give you some hard time. 

You can also use this alternative function if you want it's short in code.

<?php
function get_selected_area($strat_string,$end_string,$page_address) 
{

$whole_html = file_get_contents($page_address);
$strat_point = strpos($whole_html,$strat_string) ;
$end_point = strpos($whole_html,$end_string);                                        for($i=$strat_point;$i < $end_point;$i++)                                          $needed_part_of_html = $whole_html[$i];
return
$needed_part_of_html;
}
?>

No comments:

Post a Comment

Like this post? let me know just leave a comment..