php 正则字串
程序员文章站
2022-05-20 21:17:22
...
下面是字串
$a = '../post/luoji/soft/aaa.chm’
$b = '../post/luoji/bbb.txt'
$c = ' post/luoji/c.rar'
$d = ’../../luo/p/d.zip‘
$e = ‘../../lio/p/p/p/p/p/p/p/p/a.tar.gz’
$f = ‘../pp/tt/p/p/p/lp/p/tttt/p/t.rpm’
要求获取成这样:
$a = 'aaa.chm‘
$b = 'bbb.txt'
$c = ' c.rar'
$d = ’d.zip‘
$e = ‘a.tar.gz’
$f = ‘t.rpm’
就是不管字串有多长只要获取最后的名称
我只会这样写
$str = "../post/luoji/aaa.chm";
$str = preg_split("/[\s,\/!]+/", $str);
echo '
$a = '../post/luoji/soft/aaa.chm’
$b = '../post/luoji/bbb.txt'
$c = ' post/luoji/c.rar'
$d = ’../../luo/p/d.zip‘
$e = ‘../../lio/p/p/p/p/p/p/p/p/a.tar.gz’
$f = ‘../pp/tt/p/p/p/lp/p/tttt/p/t.rpm’
要求获取成这样:
$a = 'aaa.chm‘
$b = 'bbb.txt'
$c = ' c.rar'
$d = ’d.zip‘
$e = ‘a.tar.gz’
$f = ‘t.rpm’
就是不管字串有多长只要获取最后的名称
我只会这样写
$str = "../post/luoji/aaa.chm";
$str = preg_split("/[\s,\/!]+/", $str);
echo '
';print_r($str[3]);
不能动态获取
怎么用正则获取?谢谢!
回复讨论(解决方案)
$a = '../post/luoji/soft/aaa.chm';$a=pathinfo($a,PATHINFO_BASENAME);echo $a;$str = "../post/luoji/aaa.chm";
$str = preg_split("/[\s,\/!]+/", $str);
$len=sizeof($str);
$res=$str[$len-1];
echo $res;
$a = '../post/luoji/soft/aaa.chm';preg_match('#[^/]+$#', $a, $r);echo $r[0];$a = '../post/luoji/soft/aaa.chm';$a = preg_split('#/#', $a);echo array_pop($a);$a = '../post/luoji/soft/aaa.chm';$a = preg_split('#/#', $a);echo end($a);$a = '../post/luoji/soft/aaa.chm';echo basename($a);......