简体   繁体   English

PHP preg_match(从javascript获取网址)

[英]PHP preg_match ( get url from javascript )

To get the images url i am using this preg_match : 要获取图像的URL,我正在使用此preg_match:

preg_match( "/lstImages.push('(.+?)');/", $html, $matches );
foreach ($matches as $mt){
echo $mt;
}

in $html ther is a javascript like this : 在$ html ther中,是这样的javascript:

<script type="text/javascript">
                var lstImages = new Array();

                    lstImages.push("http://2.bp.blogspot.com/-A_8FelFBtA0/U8CbI8mDvNI/AAAAAAABHRk/bz4ysT8qeBk/046.png?imgmax=3000");

                    lstImages.push("http://2.bp.blogspot.com/-wkv19o5dCAA/U8CaDZ2VEtI/AAAAAAABHL4/mAGcV8TJbQc/001.png?imgmax=3000");

...

</script>

I want to be able to get the urls 我希望能够获得网址

http://2.bp.blogspot.com/-A_8FelFBtA0/U8CbI8mDvNI/AAAAAAABHRk/bz4ysT8qeBk/046.png?imgmax=3000

, but ther is no results ? 但是那没有结果吗?

Thanks 谢谢

'/lstImages\.push\("([^"]*)"\);/'

试试这个。这样应该可以得到所需的图像。

You should be using preg_match_all() instead if you have to do this using a regular expression. 如果必须使用正则表达式来执行此操作,则应改用preg_match_all()

The regular expression syntax is incorrect for what you are trying to match. 对于您要匹配的内容,正则表达式语法不正确。 Your pattern quotes around the delimiters, I would recommend replacing with single quotes. 您的模式引号用定界符引起,我建议您将其替换为单引号。 The single quotes inside of your expression should be double quotes instead. 表达式内的单引号应改为双引号。 Also you have meta characters that need to be escaped in order to match literal characters. 另外,您还需要转义元字符以匹配文字字符。

preg_match_all('/lstImages\.push\("(.+?)"\);/', $html, $matches);
echo implode("\n", $matches[1]);

Outputs 产出

http://2.bp.blogspot.com/-A_8FelFBtA0/U8CbI8mDvNI/AAAAAAABHRk/bz4ysT8qeBk/046.png?imgmax=3000
http://2.bp.blogspot.com/-wkv19o5dCAA/U8CaDZ2VEtI/AAAAAAABHL4/mAGcV8TJbQc/001.png?imgmax=3000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM