I’m trying to write a function in PHP that finds the first letter in a string and then returns the remainder of the string. So, for example, if you had a string:
“8932? Test 14 String”
The function would return
“Test 14 String”
I have written the following function that seems to do the job:
function removenums($withnums) {
$letters = str_split($withnums);
$position = 0;
foreach($letters as $letter) {
if(preg_match("/[a-zA-Z]/",$letter)) {
break;
}
$position++;
}
$withoutnums = substr($withnums,$position);
return $withoutnums;
}
However, I’m just learning PHP and for the sake of education and I was wondering if someone with more experience might have a better solution. All responses are appreciated. Thanks!
You can simply use
strposwithsubstrAnd you are ready to go
Please notice you should avoid whenever possibile
preg_*functions because they are generally slower than the simplerstr*functionsAfter reading your comment