How to remove the letter of an alphanumeric string
In this post, I'll try to explain how to remove the letter of an alphanumerics string.Maybe this tutorial could be helpful for you :
we have the following code:
$sMystring = 'Tra789asdf'; //1
$pMypattern = '/[a-zA-z]/'; //2
$sMyreplacement = ''; //3
echo preg_replace($pMypattern, $sMyreplacement, $sMystring);//4
Code explanation:
First Line: We have our alphanumeric string ,I mean a combination between numbers and letters.
Second Line: We have out REGEX pattern, which identify the letter in our string $pMypattern.
Third Line: We have a variable that will use to replace the letter, by that I mean instead of letter we will have in the string blank spaces.
Fourth Line: We will have our output and it is important to note the preg_replace
which identified in the string the pattern and remove the matches and replace with the new value in our case is: $Myreplacement.
Output : 789
If want the opposite output, I mean remove the number the code will be:
$sMystring = 'Tra789asdf';//1
$pMypattern = '/[0-9]/';//2
$sMyreplacement = ''; //3
echo preg_replace($pMypattern, $sMyreplacement, $sMystring); //4
Output :: Traasdf
No comments:
Post a Comment