Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
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
 
Old July 16th, 2004, 05:53 AM
Authorized User
 
Join Date: Jul 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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!
 
Old July 16th, 2004, 09:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old July 16th, 2004, 11:16 AM
Authorized User
 
Join Date: Jul 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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!
 
Old July 16th, 2004, 11:17 AM
Authorized User
 
Join Date: Jul 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hmm, come to think of it .. Flat 2B, 23 Grant street would leave me with .. 223 instead of 23. Hmmmmm..

* get's documentation out *
 
Old August 6th, 2004, 08:13 AM
Authorized User
 
Join Date: Jul 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?

 
Old August 6th, 2004, 10:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old August 6th, 2004, 11:18 AM
Authorized User
 
Join Date: Jul 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ah yes, I forgot to say, that one gives me a parse error, I tried:
$addrnum = sscanf("$address", %d);
and
$addrnum = sscanf($address, %d);

 
Old August 7th, 2004, 07:49 AM
Authorized User
 
Join Date: Jul 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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++;
        }
?>





Similar Threads
Thread Thread Starter Forum Replies Last Post
assign numerical values to text ptrussell2009 Excel VBA 9 October 8th, 2017 04:38 AM
Extract part of String carrie09 Access 6 August 24th, 2007 10:04 AM
how to extract from a string fraijo SQL Server 2000 3 October 25th, 2006 09:55 PM
Getting Numerical Values Fiddleman HTML Code Clinic 2 September 13th, 2004 02:51 PM
textbox with numerical values must C# 1 August 7th, 2003 12:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.