javascriptphpweb-scrapingpreg-match

preg_match not Working php and java script


I need to extract a value (id value) from html web page . this value is included in JavaScript code.

my scraper php >

<?php
  if (isset($_POST['submit']))
  {
      $handle = fopen($_POST['website_url'], "r");
      if ($handle)
      {
          while (!feof($handle))
          {
              $text .= fread($handle, 128);
          }
          fclose($handle);
      }
      $result = preg_match('(?<=id: ")(.*)(?=",)', $text);

      echo $result;

  }

?>

My javaScript Code is >

<script type="text/JavaScript">

var geoInstance = gioFinder("geo");
geoInstance.setup({
    id: "126568949", // i need this
    geometre: "triangle",
    type: "html",
    image: 'https://example/126568949.jpg',
});

</script>

I need to extract id value but preg_match not working


Solution

  • It looks like you are missing the /s wrapping your pattern.

    You will also want to set the matches array

    $result = preg_match('/(?<=id: ")(.*)(?=",)/', $text, $matches);
    
    echo $matches[0];