delete everything after last /

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

delete everything after last /

Post by phice »

I'm looking for some function that will delete everything after the last backslash (/), including the backslash.

In example;
before: /home/to/folder
after: /home/to
Image Image
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

function delete_backslash($str){
$array = explode("/", $str);
$key = count($array) - 1;
unset($arrayї$key]);
$str = implode("/", $array);
return $str;
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

or simply

Code: Select all

<?php
$before = '/home/to/folder';
$after = substr($before, $after, strrpos($before, '/'));
echo $after;
?>
or if it might be that there is no / in the string

Code: Select all

<?php
$before = 'hometofolder';
if($pos=strrpos($before, '/'))
	$after = substr($before, $after, pos);
echo $after;
?>
superwormy
Forum Commoner
Posts: 67
Joined: Fri Oct 04, 2002 9:25 am
Location: CT

Post by superwormy »

or:

$before = "/home/Administrator/file";

$temp = strrev ($before);
$temp = strstr ($temp, "/");
$temp = substr ($temp, 1);

$after = strrev ($temp);
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Post by mchaggis »

A much simpler method is:

Code: Select all

$after = dirname($before);
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

way too easy ;)
Post Reply