Given a registration code, or something important like that, which often contains information encoded in the alphanumeric string itself, it is sometimes quite useful to be able to simply strip out the letters from the string, leaving only the numeric part behind.
So for example, for a code that looks like LTTCRA003 (my old University student number in case you were wondering), it might be of use to pull out the digit part as that information tells you how many times the LTTCRA hash of name and surname has occurred before.
Now in order to grab the numbers we are simply going to knock out all the letters contained in the string and for that we will use preg_replace, a function which makes use of regular expression matching to make its string replacements.
So what exactly do we need to do in terms of code then?
Well, the regular expression itself is pretty simple (probably the most simple you can get), and put into code, this is what will solve our little problem for us:
$numbers = preg_replace('/[a-zA-Z]/','','LTTCRA003');
The result of this code will be ’003′, thanks to us replacing all the letters with blanks.
And there you go. That’s one way of getting all the numbers out of an alphanumeric string! :)
You might also enjoy:
-
Given a registration code, or something important like that, which often contains information encoded in the alphanumeric string itself, it is sometimes qui ...
-
Sometimes it comes in quite handy to strip out all the non-alphanumeric characters from a given string. Of course, we could just use a bog standard preg_rep ...
-
Today's little PHP hint is a pretty simple one, but one worth tackling if you are looking for a quick and easy way to strip a particular string pattern off ...
-
Youve often seen these long, seemingly random numbers in secured web pages and perhaps wondered just what the heck that is meant to be. Well, usually these ...
-
If you come from a VB programming background, often you are left floundering when trying to retrieve character codes from a string or vice versa, trying to ...