Removing the first slash character from a PHP string

I have a line of this format /abcd-efg-sms-3-topper-2

I want to remove from this first character / .

I know that I can remove this with the substr() function, but I do not want to use this, since I need to check first if the first character is a slash. Is there any other way to remove a slash without first checking for a slash and thus be efficient enough (i.e. Avoid complex regular expressions)?

+4
source share
1 answer

Use cropping:

 $string = '/abcd-efg-sms-3-topper-2'; echo ltrim($string, '/'); // abcd-efg-sms-3-topper-2 echo rtrim($string, '/'); // /abcd-efg-sms-3-topper-2 echo trim($string, '/'); // abcd-efg-sms-3-topper-2 
+17
source

Source: https://habr.com/ru/post/1435187/


All Articles