0

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>
valiano
  • 16,433
  • 7
  • 64
  • 79
Boy
  • 582
  • 1
  • 5
  • 22

2 Answers2

1

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);
0

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;
LF00
  • 27,015
  • 29
  • 156
  • 295