dragoninasia2014 2013-07-08 01:21
浏览 21
已采纳

过滤这些字符串PHP的最佳方法

i am doing some DOM parsing, and i have some strings i have to clean, they look like this:

$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()"

etc etc, you can see the system, i only need the last URL in the string, the amount of strings is variable, and the amount of links inside the strings is aswell, but i only need the last link in each string.

Should i use REGEX or a series of explode ?

  • 写回答

3条回答 默认 最新

  • dongyong8071 2013-07-08 01:30
    关注

    With explode

    $arr = explode(';',$str);
    $arr = $arr[count($arr) - 2]; // get the last link
    $arr = trim($arr,"arrayImg[0]="); //here you will get only the last link
    

    Live Demo

    Most of people stuck in situation whether they have to use regex or any other predefined functions. You have to use predefined functions if your task can be accomplished with them otherwise use regex if none of them available to accomplish your task.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助