PDA

View Full Version : split string at last '/'


mindlessLemming
11-04-2004, 04:01 AM
Yep, me again... :o
Regex confuses the #!@% out of me and I'm not even sure if that's what I should be using for this task, so here goes:

I am looking for a way to strip a string back to the last occurence of '/'.
eg: 'http://yoursite.com/blog/index.php' would become 'http://yoursite.com/blog/'

Little help? :)

subhailc
11-04-2004, 12:12 PM
uh this is probably useless and i don't even know what regex is much less the syntax, but in the 1% chance it's not

charAt(index);
slice(index);

here's an e.g. but it's really badly written, it'll work but give a few errors :/

<html>
<head>
<script>
var e=0;
function n(k){
e=k.length;
f=setTimeout(o(k),100);
}
function o(k) {
l=k.charAt(e);
if (l=="/"){
v=k.slice(e,k.length);
i.innerText=v;
clearTimeout(t);
}else{
e--;
t=setTimeout(o(k),100);
}
}
</script>
</head>
<body>
<button onclick="n('http://yoursite.com/blog/index.php');">button</button><span id="i"></span>
</body>
</html>

raf
11-04-2004, 01:27 PM
you don't need a regex. Just

$url = 'http://yoursite.com/blog/index.php';
$base = substr($url, 0, strrpos($url,'/' ));

mindlessLemming
11-04-2004, 10:48 PM
Cheers Raf :D