[HOW TO]: Fetch a certain part of HTML, Text or PHP From Outer Source With PHP
I am going to teach you how to fetch certain part of HTML, Text or PHP for any outer source(i.e. From a website). Please read all the contents clearly. You most have a problem to fetching a certain part of HTML or PHP because Text files are more easy to handle. We here do not fetch the PHP code but we fetch the HTML code after the code is published in a website. Those code are HTML codes most and your can easily view in the source. But the main problem with the user are they cannot fetch the code although all the code is found to be correct. The code that I use is given below:
<?php
$contents = file_get_contents(‘http://www.example.com’);
$term_search=”The term to be searched”;
$no_term=1000;
$pos=strpos($contents, $term_search);
$pos += strlen($term_search);
$new_pos = substr($contents,$pos,$no_term);
echo $new_pos;
?>
The above PHP code has different parameters. Please continue reading. The above code functions as such: first get all the code from the outer source. Then it calculates the position of the found string. Then it add the number of strings searched and then get the string found there after the specified postion and number of characters. Finally it displays the specified part. Here is the description of the above code:
//This code get the HTML code from the website that you specified
$contents = file_get_contents(‘http://www.example.com’);
//This place is the user specified place. Please find the more note below. Most of the people make some bug here.
$term_search=”The term to be searched”;
//This place is the place to specify how much term that you want to display after all the calculation
$no_term=1000;
//This finds the position of the specified text that is searched
$pos=strpos($contents, $term_search);
//This is an Optional. This can be removed if
$pos += strlen($term_search);
//This post get the content from the $content from the position $pos with number of terms $no_term
$new_pos = substr($contents,$pos,$no_term);
//That’s you know.
echo $new_pos;
*The note for the “$term_search”:
Most of we have a problem regarding the HTML and PHP because the have some tags like “<”, “>”, etc.
And there is a problem searching such type of contents. Those tags are called HTMLSPECIALCHARS that have their own code in HTML like for “<” we assume “<” likewise for “>” we assume “>”. As more you can find it easily.
For an example: if there is such a code in HTML as “Welcome people this is a new world. Here is a bold example.” In this example lets assume we need to fetch a code after the word “world” which is underlined. Then lets view its HTML code of the example. Then it seems to be as “Welcome people this is a new <u>world</u>. Here is a bold example.” but that’s not the ultimate solution. We need to do more as the code has more HTML things as, “Welcome people this is a new <u>world</u>. Here is a bold example.”. Now you must replace the code as $term_search=”<u>world</u>”; and finally you get the result.
hi there what if the webpage changes inclemently constantly like http://www.example.com/1.html http://www.example.com/2.html and so on would this php code work in that case too? Also what if i want to save the page if the text like “hello” is found in the page if not move to next page? thanks in advance
Dear Jeevan,
The final output is stored in “$new_pos” and it is the complete set of text present in the content fetched from site. We have a PHP function named “strpos” which checks the position of the content like:
$aslipos=strpos($newpos, “Your text as hello”);
and it gets the position of the text that u have searched like “hello”. And next line code would be:
if ($aslipos) {
echo “This means the searched text is in the website”;
}
else {
echo “This means the searched term is not found in the website then you can kep the code that you like.”;
}
If you had any problem then you can comment here for that.
Awesome post. Help for my development.
The author of http://www.hamrobrt.com has written an excellent article. You have made your point and there is not much to argue about. It is like the following universal truth that you can not argue with: Intelligence is knowing that a tomato is a fruit, wisdom is not putting it in a fruit salad
Thanks for the info.
very nice post, thanks
Hey hun, sweet website! I really appreciate this blog post.. I was curious about this for a while now. This cleared a lot up for me! Do you have a rss feed that I can add?
Can I pick the search bar from your site hamrobrt.com if yes then how? Please tell the process.
Yes of course. But not by PHP code but by the HTML code of the search bar.
Great tut. I was thinking in different way.How about creating Socket to connect to site and fetch some HTML contain that would be easy . Like fopen($site). Just an alternate.
@gandip
Thanks for the info. Nice
nice info!! thanks