简体   繁体   中英

Remove part of string after third occurrence of certain character

I have a special case where I have a whole range of different strings in an array.

What I need is to remove last part of a string at the third occurrence of /.

Example:

Accessories / Mens Accessories / Bags Wallets / Messenger Bags /

So here I want to remove: Messenger Bags /

How is this possible with PHP - remember I have a lot of different strings! So I can't take the position as it's different each time.

//Here you will have 4 elements    
$parts = explode ('/', $string);

//this will glue the first 3 elements
list ($first, $second, $third) = $parts;

//Here you can see the desired result
var_dump (implode ('/',array($first, $second, $third)));

Output

string 'Accessories / Mens Accessories / Bags Wallets ' (length=46)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM