...

View Full Version : finding numbers in a string



bhakti_thakkar
07-16-2007, 08:59 AM
hi all,
is there a simple way for extracting numbers from a string. i have a session array in which the data is like this "AB000121 --> nameof the client"
wherein i just want to extract "000121" where 0000121 could be any number from 0 to sky is the limit [just the numbers]... how can i do it?? :rolleyes:

Thanks in advance

_Aerospace_Eng_
07-16-2007, 09:09 AM
Just use preg_replace()

<?php
$string = 'AB000121';
$newstring = preg_replace('/[a-zA-Z]/xms','',$string);
echo $newstring;
?>

daemonkin
07-16-2007, 09:21 AM
Aerospace: Can you explain the use of xms in your preg_replace?

D.

_Aerospace_Eng_
07-16-2007, 09:28 AM
You can look up these things yourself you know?
Trailing xms are three regex options
x : Extended formatting (whitespace in regex is ignored)
m : For line boundaries (and eliminates a cause of some subtle
bugs)
s : ensures everything is matched by the "." symbol
http://www.ilovejackdaniels.com/cheat-sheets/regular-expressions-cheat-sheet/

bhakti_thakkar
07-16-2007, 10:14 AM
Hi Aerospace,
the code works the way i want.

Just use preg_replace()

<?php
$string = 'AB000121';
$newstring = preg_replace('/[a-zA-Z]/xms','',$string);
echo $newstring;
?>

Thanks a lot :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum