我正在做一些DOM解析,我有一些字符串需要清理,它们看起来像这样:
$str1 = "var arrayImg=new Array();arrayImg[0]=
http://somepage.com/2013-5-11/1/1.jpg
;getImgString()";
$str2 = "var arrayImg=new Array();arrayImg[0]=
http://somepage.com/2013-5-11/1/1.jpg
;arrayImg[0]=
http://somepage.com/big/qingliang/2013-5-11/1/2.jpg
;getImgString()"
$str3 = "var arrayImg=new Array();arrayImg[0]=
http://somepage.com/2013-5-11/1/1.jpg
;arrayImg[0]=
http://somepage.com/2013-5-11/1/2.jpg
;arrayImg[0]=
http://somepage.com/2013-5-11/1/3.jpg
;getImgString()"等等,你可以看到系统,我只需要字符串中的最后一个URL,字符串的数量是可变的,字符串中的链接数量也是可变的,但我只需要每个字符串中的最后一个链接。
我应该使用REGEX还是一系列的explode?
发布于 2013-07-08 09:31:36
如果字符串包含一致的模式,use explode(),它将变得非常简单,而且您不必担心正则逻辑的风险。否则请使用regex。
发布于 2013-07-08 09:41:55
如果你想要正则表达式(而不是分解字符串),试试这个:
preg_match_all ("/http\S+/", $str, $matches);
$link = $matches[0][count($matches[0])-1];UPD发现错误,代码已更新
https://stackoverflow.com/questions/17517994
复制相似问题