如何preg_match特定的javascript元素来获取值?

时间:2014-12-21 21:11:04

标签: php web-scraping simple-html-dom

示例:

<script>
var images= new Array();
   lstImages.push("one");
   lstImages.push("two");
   lstImages.push("three");
</script>

如何preg_match所以我可以获得值1,2,3的结果?

1 个答案:

答案 0 :(得分:1)

$html = '<script>
var images= new Array();
   lstImages.push("one");
   lstImages.push("two");
   lstImages.push("three");
</script>';

$matches = array();
if(preg_match_all('#lstImages.push\("(.*)"\);#', $html, $matches))
{
    var_dump($matches[1]);
}

这将返回:

array (size=3)
  0 => string 'one' (length=3)
  1 => string 'two' (length=3)
  2 => string 'three' (length=5)

所以,我们基本上在搜索字符串lstImages.push("something here");

然后你必须像这样利用它:$items = $matches[1];

然后您的所有商品都在$items