Home > Backend Development > PHP Tutorial > php 正则字串,该怎么处理

php 正则字串,该怎么处理

WBOY
Release: 2016-06-13 11:56:12
Original
955 people have browsed it

php 正则字串
下面是字串

     $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]);<br><br>    不能动态获取<br><br>    怎么用正则获取?谢谢!<img src="/img/2014/05/02/11500367.gif" alt=""><br><br><font color="#FF8000">------解决方案--------------------</font><br><pre class="brush: php"><br />$a = '../post/luoji/soft/aaa.chm';<br />$a=pathinfo($a,PATHINFO_BASENAME);<br />echo $a;<br />
Copy after login

------解决方案--------------------
$a = '../post/luoji/soft/aaa.chm';<br />preg_match('#[^/]+$#', $a, $r);<br />echo $r[0];
Copy after login
$a = '../post/luoji/soft/aaa.chm';<br />$a = preg_split('#/#', $a);<br />echo array_pop($a);<br />
Copy after login
$a = '../post/luoji/soft/aaa.chm';<br />$a = preg_split('#/#', $a);<br />echo end($a);<br />
Copy after login
$a = '../post/luoji/soft/aaa.chm';<br />echo basename($a);<br />
Copy after login
......
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template