简体   繁体   中英

PHP preg_match ( get url from javascript )

To get the images url i am using this preg_match :

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

in $html ther is a javascript like this :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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