Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing first slash character from string PHP [closed]

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

I want to remove the first / character from this.

I know I can remove this using substr() function, but I do not want to use that, since I first have to check if first character is the slash. Is there any other way I can remove the slash, without first checking for the slash, and have that way be reasonably performant (i.e. avoiding complex regular expresions)?

like image 951
Yogesh Suthar Avatar asked Sep 20 '12 05:09

Yogesh Suthar


People also ask

How do I strip a slash in PHP?

The stripslashes() function removes backslashes added by the addslashes() function. Tip: This function can be used to clean up data retrieved from a database or from an HTML form.

How do I remove the first character of a string in PHP?

You can use the PHP ltrim() function to remove the first character from the given string in PHP.

How do I trim a string after a specific character in PHP?

The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string. rtrim() - Removes whitespace or other predefined characters from the right side of a string.

How do I remove the first and last character of a string in PHP?

Using trim : trim($dataList, '*'); This will remove all * characters (even if there are more than one!) from the end and the beginning of the string.


1 Answers

Use trim:

$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
like image 79
Sergey Avatar answered Sep 19 '22 13:09

Sergey