Page 1 of 1

Purifying number from MySQL query

Posted: Tue Aug 20, 2002 12:23 pm
by pbsperry
What is the correct process for "purifying" a number that's returned from a query in mysql? I've got a field in a table that has a number that I want to execute a function on, but also in the same field is usually text and other characters. Is there any way in PHP to "purify" the number so that the function just runs on the number itself? The field is a text field (that's the way I had to set it up to accept imports from our mainframe), and it contains entries such as: (3000)DT Thomas, or (2500). I want to execute the function on only the number part (the "3000" or "2500" respectively).

Posted: Tue Aug 20, 2002 1:46 pm
by Johnm
I think I understand what you want.
Get the string length using substr_count(),
Parse the string using a loop with its length (use substr(string length)) check each element of the sub string array to see if is a number (you could multiply by 1 and see if it is equal to itself)
and if it is concatenate the numbers and place into a variable to operate on.

There are no built-in db functions that I know of to do want you want to do.

Did I understand your question properly?

Direwolf

Posted: Tue Aug 20, 2002 2:00 pm
by nielsene
If its always a four digit number enclosed in parenthesis you could use something like

Code: Select all

$number = substr($jumbledMess,strpos($jumbledMess,"(")+1,4);
To be a little cleaner you can grab the positions of both the starting and ending parenthesis and then calculate the number of chracters to extract, instead of hard-coding the four.

If the data isn't that consistent, you can always use ereg/preg