Purifying number from MySQL query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
pbsperry
Forum Newbie
Posts: 6
Joined: Thu Aug 15, 2002 1:27 pm

Purifying number from MySQL query

Post 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).
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post 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
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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
Post Reply