0

我有一个包含 Javascript 代码的 HTML 文件:

<SCRIPT language="javascript" type="text/javascript">
var statList = new Array(
0, "192.168.1.179", "88-53-95-28-2B-AF", 194280, 101141053, 0, 0, 0, 0, 0, 0, 0, 0, 
1, "192.168.1.170", "60-C5-47-10-37-FD", 132316, 65860791, 0, 0, 0, 0, 0, 0, 0, 0, 
2, "192.168.1.151", "68-5D-43-21-76-95", 9887, 3898646, 0, 0, 0, 0, 0, 0, 0, 0, 
3, "192.168.1.134", "6C-3E-6D-8C-FF-62", 26405, 7521875, 0, 0, 0, 0, 0, 0, 0, 0, 
4, "192.168.1.124", "04-F7-E4-78-D3-D0", 6189, 1791672, 0, 0, 0, 0, 0, 0, 0, 0, 
5, "192.168.1.121", "60-C5-47-10-40-92", 122657, 91113301, 0, 0, 0, 0, 0, 0, 0, 0);
</SCRIPT>

我想从此文件中获取内容。这是javascript中的一个数组,即“StatList”。我想在一个数组中选择所有 IP 地址,如(192.168.1.179),在另一个 PHP 数组中选择所有 Mac 地址,如(88-53-95-28-2B-AF)。

我怎样才能做到这一点?

4

1 回答 1

1

如果您将 HTML 作为字符串,您可以使用如下正则表达式来抓取它:

<?php

    $html = '<SCRIPT language="javascript" type="text/javascript">
    var statList = new Array(
    0, "192.168.1.179", "88-53-95-28-2B-AF", 194280, 101141053, 0, 0, 0, 0, 0, 0, 0, 0, 
    1, "192.168.1.170", "60-C5-47-10-37-FD", 132316, 65860791, 0, 0, 0, 0, 0, 0, 0, 0, 
    2, "192.168.1.151", "68-5D-43-21-76-95", 9887, 3898646, 0, 0, 0, 0, 0, 0, 0, 0, 
    3, "192.168.1.134", "6C-3E-6D-8C-FF-62", 26405, 7521875, 0, 0, 0, 0, 0, 0, 0, 0, 
    4, "192.168.1.124", "04-F7-E4-78-D3-D0", 6189, 1791672, 0, 0, 0, 0, 0, 0, 0, 0, 
    5, "192.168.1.121", "60-C5-47-10-40-92", 122657, 91113301, 0, 0, 0, 0, 0, 0, 0, 0);
    </SCRIPT>';
    $regex = '/\d+,\s+"(?P<ip>[\d\.]+)",\s+"(?P<mac>[\-\dA-F]+)",/';

    $matches = array();
    preg_match_all($regex, $html, $matches);
    echo "<pre>";
    print_r($matches['ip']);
    print_r($matches['mac']);

?>

工作示例

于 2013-02-01T09:46:01.593 回答