 |
| Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 16th, 2004, 05:53 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How to extract numerical values from string?
I'm trying to extract all numerical values from a string (a user's address).
I don't know where to start... Could someone show me an example of what I need to do?
Input, for example: 23 grant street
Output: 23
Thanks!
|
|

July 16th, 2004, 09:56 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Well, if you want to play fast and loose with PHP's implicit type conversion, you can take:
$addressline = "Flat B 23 grant street";
$addressnumber = (int)$addressline;
Casting to type int, will cause the PHP engine to extract any recognisable integer value form the string (i.e. "23").
A more sensible approach might be to use the sscanf function to hunt for what you're after
( http://uk2.php.net/manual/en/function.sscanf.php), as in:
$housenumber = sscanf("Flat B, 23 grant street", %d);
$flat_if_any = sscanf("Flat B, 23 grant street", "Flat %s"); //Will parse-out "Flat 2A" "Flat IX", etc.
The "%." things are type specifiers. The page for sprintf has a list of them ( http://uk2.php.net/manual/en/function.sprintf.php)
Take it easy,
Dan
|
|

July 16th, 2004, 11:16 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you Daniel!
I don't think it has to be too robust, though I should look it up in the documentation of the credit card processor that wants this info. I never thought of things like "Flat B".. The information is needed for an AVS (Automatic Verification System) for a credit card company and I think that fraud checking only happens on a numerical level, the first option should work perfectly there!
Thanks again!
|
|

July 16th, 2004, 11:17 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hmm, come to think of it .. Flat 2B, 23 Grant street would leave me with .. 223 instead of 23. Hmmmmm..
* get's documentation out *
|
|

August 6th, 2004, 08:13 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Alright, it's been a few weeks, but... typecasting doesn't work, it always gives me a value of 0 (as opposed to 23 or whatever)... any other way?
|
|

August 6th, 2004, 10:50 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How about my alternative suggestion of scanf?
As in:
$housenumber = sscanf("Flat B, 23 grant street", %d);
You're instructing scanf to retrieve any decimal value from the searched string.
Take it easy,
Dan
|
|

August 6th, 2004, 11:18 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ah yes, I forgot to say, that one gives me a parse error, I tried:
$addrnum = sscanf("$address", %d);
and
$addrnum = sscanf($address, %d);
|
|

August 7th, 2004, 07:49 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I solved it this way, not elegant but it extracts all the numerical values, which is exactly what is needed according to the credit card processor's specs:
<?php
$addresslength = strlen($address);
$i = 1;
$addrnum = "";
while ($i <= $addresslength) {
$curChar = substr($address,$i,1);
if (ereg("[0-9]", $curChar, $reg)) { $addrnum = $addrnum . $reg[0]; }
$i++;
}
?>
|
|
 |