Php regular string

Source: Internet
Author: User
Optional & nbsp; php regular string
Below is the string

$ A = '../post/luoji/soft/aaa. chm'
$ B = '../post/luoji/bbb.txt'
$ C = 'post/luoji/c.rar'
$ D = '../luo/p/d.zip'
$ E = '../lio/p/a.tar.gz'
$ F = '../pp/tt/p/lp/p/tttt/p/t. rpm'

The request is as follows:

$ A = 'AAA. chm'
$ B = 'bbb.txt'
$ C = 'c.rar'
$ D = 'd.zip'
$ E = 'a.tar.gz'
$ F = 'T. rpm'


That is, no matter how long the string is, you only need to get the final name.

I will write it like this

$ Str = "../post/luoji/aaa. chm ";
$ Str = preg_split ("/[\ s, \/!] +/", $ Str );
Echo'
'; Print_r ($ str [3]);

Cannot be dynamically obtained

How can I use regular expressions? Thank you!

------ Solution --------------------

$a = '../post/luoji/soft/aaa.chm';
$a=pathinfo($a,PATHINFO_BASENAME);
echo $a;

------ Solution --------------------
$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);
......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.