how to get value between the script tag using php scrapping.
$homepage = file_get_contents('http://www.example.com/');
//$homepage
<script type="application/ld+json">
//FETCH ME
/script>
how to get value between the script tag using php scrapping.
$homepage = file_get_contents('http://www.example.com/');
//$homepage
<script type="application/ld+json">
//FETCH ME
/script>
This seems like the answer you need here, Get content between two strings PHP
The second answer seems easiest:
$out = file_get_contents('http://www.example.com/');
$start = "<script ...>"; // replace ... with exact other text you are maching
$end = "</script>";
$startsAt = strpos($out, $start) + strlen($start);
$endsAt = strpos($out, $end, $startsAt);
$result = substr($out, $startsAt, $endsAt - $startsAt);
with $result
being the as it says result of the content between $start
and $end
For multiple instances, just delete the first occurance from $out
and repeat:
$out = str_replace($start.$result.$end,"",$out);
The first occurance is removed, so you can retreive the second occurance. But sure, there may be a more simpler way to get all occurances, not just this.
$startsAt = strpos($out, $start) + strlen($start);
$endsAt = strpos($out, $end, $startsAt);
$result = substr($out, $startsAt, $endsAt - $startsAt);
You can use domDocument to parse the html page.
For example,
$response = file_get_contents($targetpath);
$dom = new domDocument;
$dom->preserveWhiteSpace = false;
@$dom->loadHTML($response);
$description = $dom->getElementById('domid');
$description_text = $description->childNodes->item(1)->childNodes->item(3)->nodeValue;