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)?
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.
You can use the PHP ltrim() function to remove the first character from the given string 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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With