【问题标题】:preg_match not Working php and java scriptpreg_match 不工作 php 和 javascript
【发布时间】:2020-01-30 19:59:15
【问题描述】:

我需要从 html 网页中提取一个值(id 值)。 此值包含在 JavaScript 代码中。

我的爬虫 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;

  }

?>

我的 JavaScript 代码是 >

<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>

我需要提取 id 值但 preg_match 不起作用

【问题讨论】:

  • 请注意 java != javascript

标签: javascript php preg-match scrape


【解决方案1】:

您似乎缺少包裹您的模式的 /s。

您还需要设置匹配数组

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

echo $matches[0];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    相关资源
    最近更新 更多