Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove first forward slash in a link?

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!

like image 644
0plus1 Avatar asked Jun 05 '09 10:06

0plus1


People also ask

How do you remove a slash from the beginning of a string?

Use the String. replace() method to remove a trailing slash from a string, e.g. str. replace(/\/+$/, '') . The replace method will remove the trailing slash from the string by replacing it with an empty string.

How do you escape the front slash?

Slashes. The forward slash character is used to denote the boundaries of the regular expression: ? The backslash character ( \ ) is the escaping character.


1 Answers

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.

like image 56
Stefan Gehrig Avatar answered Oct 02 '22 14:10

Stefan Gehrig