简体   繁体   中英

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!

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.

preg_replace('/^\//', '', $link);

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

$uri = "/directory/link.php";
$uri = substr($uri, 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM