tags:

views:

24

answers:

1

Hi All,

We have a query(mysql) which returns data in the following format of alphabets followed by digits followed by alphabets like -- text1 12.12 mg text2

Now the issue is , i need to write a script in php which gives all the data starting from the 1st digit.so the result should be something like

12.12 mg text2

I am not sure how to accomplish this in php and the functions which might be of use for this purpose. Any help would be appreciated.

+5  A: 
preg_match('#\\d+(.*)$#', $message, $match);
$text = $match[1];

the \\d+ means one or more consecutive digits. (.*) means match any character except a new line. $ tells it to go to the end of the string...

ircmaxell
thank you very much.also does this accept special characters like %
swathi
Shouldn't it be just `\d+`? I mean only one `\\` ?
Felix Kling
+1: MySQL doesn't have the functionality to do this easily.
OMG Ponies
@swathi yes, the `.` matches any character except `\n`... @Felix Kling yes and no, the double ` \\ ` just forces a literal ` \ ` inside of the string. With a single quote it's redundant, but I'm just in the habit of always doing it. @OMG Ponies: Right on. It has good REGEXP support for where clauses, but very little for fields...
ircmaxell
@ircmaxell,when i am trying to use this code it gives me all the characters after the first few digits,for ex:it gives me - mg text2 instead of 12.12 mg text2.any suggestions.
swathi
Are you sure there's a space after the digit? If it's only ever a single digit, you could change the regexp to `'#^\\D*\\d(.*)$#'`, which says any number of non-digit characters from the start of the string (`^\\D`), then a single digit character (`\\d`) then anything following up to the end...
ircmaxell