...

View Full Version : Removing charachers from end of string



tonyyeb
01-25-2008, 09:19 PM
Hi all

I have this string:

http://www.whatever.com/testing/index.php

I want to remove whatever is AFTER the last slash. So in the example above it would remove index.php

Thanks in advance!

arnyinc
01-25-2008, 10:05 PM
$url='http://www.whatever.com/testing/index.php';

$trunc_url=substr($url, 0, strrpos($url, '/')+1);


You can perform the substr() and strrpos() calls on two different lines for readability.

JohnDubya
01-25-2008, 10:06 PM
Here's what you need to learn this:

strrpos() - http://us3.php.net/strrpos

substr() - http://us.php.net/substr

Basically, find the position of the last / in that string with strrpos(), and then substr() up to that / , and you've got what you need. Play around with it to get it how you need. If you need further assistance, just let us know! :)

oesxyl
01-25-2008, 10:11 PM
Hi all

I have this string:

http://www.whatever.com/testing/index.php

I want to remove whatever is AFTER the last slash. So in the example above it would remove index.php

Thanks in advance!

check your php version if you use this:



$url = "http://www.whatever.com/testing/index.php";
echo parse_url($url, PHP_URL_PATH);


and read:

http://www.php.net/manual/en/function.parse-url.php

another way:



$url = "http://www.whatever.com/testing/index.php";
$url = preg_replace("/[^\/]*$/","",$url);
echo $url;


both not tested, :)

best regards

tonyyeb
01-25-2008, 11:07 PM
$url='http://www.whatever.com/testing/index.php';

$trunc_url=substr($url, 0, strrpos($url, '/')+1);


You can perform the substr() and strrpos() calls on two different lines for readability.

Thanks everyone. This post lead me to the solution. Thanks all!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum