0

我需要从 html 网页中提取一个值(id 值)。该值
包含在 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>

我的代码 php 是

    <?php
    $filename = "https://examlpe.com";
    $handle = fopen($filename, "r");
    if ($handle) {
        while (!feof($handle)) {
            $text. = fread($handle, 128);
        }
        fclose($handle);
    }

    // How can you do

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

echo $matches[0];
//end

    ?>
4

1 回答 1

2

您可以将文件作为字符串读取,然后使用 RegEx 查找特定值。

您的案例的一个示例可能是 RegEx 模式(?<=id: ")(.*)(?=",)

然后使用preg_matchpreg_match('(?<=id: ")(.*)(?=",)', $html);

于 2020-01-30T18:33:30.183 回答