searching the html source code of a url using curl in php
i am new to curl (php). by new i mean i just started 20 minutes ago. the aim is to grab the html source code of a url provided, search for a given word and extract the next 5 lines of code.
so far all i know is how to get the source code. not search for a string and get next 5 lines. so far what i have is this
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.google.com");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch,开发者_如何学C CURLOPT_RETURNTRANSFER, 1);
$content=curl_exec($ch);
$htmlcontent = htmlspecialchars($content);
echo $htmlcontent;
curl_close($ch);
for example, take google.com. i want to get its source code, search for the exact word 'status:' and extract/echo the next five lines after its first occurence.
php has multiple functions to search strings, for example strpos
. You can use it to find the index of status
in the downloaded string. You can it again to find newlines from that index on ("\n"
).
When you have determined the start and last index of the string you want to extract, use substr
to actually extract it.
精彩评论