If I have the string like "sus34234" how can I first check if the first character is a "s" and then if it is strip the first character and return the new string.
I'd probably do:
$string = preg_replace('/^s/', '', $string);
If you want the search/replace to be case-insensitive, change the regexp pattern to '/s/i'.
If you want to do this in two steps without using a regular expression, you could do:
if(substr($string, 0, 1) == 's') { $string = substr($string, 1); }
Or yet another alternative:
$string = ltrim($string, 's');
But this too makes the assumption that the s is lowercase only. As usual, there's many ways to skin a cat.
nrg_alpha;10926279 wrote:Or yet another alternative: $string = ltrim($string, 's'); But this too makes the assumption that the s is lowercase only. As usual, there's many ways to skin a cat.
Note that this would change "ssx1234" to "x1234". If this is the desired effect, then this is a nice solution. If however it should be changed to "sx1234", then one of the other solutions would better.
NogDog;10926321 wrote:Note that this would change "ssx1234" to "x1234". If this is the desired effect, then this is a nice solution. If however it should be changed to "sx1234", then one of the other solutions would better.
Good catch, Nog.. forgot that it will strip consecutive characters in the trim parameter.
Meanwhile, of course substr($string, 0, 1) could be written as $string[0] - if one can assume that $string really is at least one character long.