views:

1671

answers:

4

I need to remove the first forward slash inside link formatted like this:

/directory/link.php

I need to have:

directory/link.php

I'm not literate in regular expressions (preg_replace?) and those slashes are killing me..

I need your help stackoverflow!

Thank you very much!

+4  A: 
preg_replace('/^\//', '', $link);
duckyflip
Thank you very much, can you point me to a good learning resource for preg_replace?
0plus1
+2  A: 

If it's always the first character, you won't need a regex:

$uri = "/directory/link.php";
$uri = substr($uri, 1);
karim79
You don’t need to specify the length.
Gumbo
@Gumbo - Doh! Thanks
karim79
Thanks, I completly lost my perspective.. and overlooked this simple method..
0plus1
A: 

Thank you very much, can you point me to a good learning resource for preg_replace?

best place to get to know preg_replace is of course php documentation I'd suggest checking general preg (pcre) documentation as well.

Michal M
The php documentation lacks depth in regards of regular expression, there are no throughtfully explanation of expression such as the one used by duckyflip!
0plus1
+8  A: 

Just because nobody has mentioned it before:

$uri = "/directory/link.php";
$uri = ltrim($uri, '/');

The benefit of this one is:

  • compared to the substr() solution: it works also with paths that do not start with a slash. So using the same procedure multiple times on an uri is safe.

  • compared to the preg_replace() solution: it's certainly much more faster. Actuating the regex-engine for such a trivial task is, in my opinion, overkill.

Stefan Gehrig
darn. as soon as I saw this question I was hoping I'd get to suggest this. :) +1
Paolo Bergantino
ltrim($uri,chr(47));
Jabes88