Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 16th, 2004, 05:53 AM
Authorized User
 
Join Date: Jul 2004
Location: , , .
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!
Reply With Quote
  #2 (permalink)  
Old July 16th, 2004, 09:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
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
Reply With Quote
  #3 (permalink)  
Old July 16th, 2004, 11:16 AM
Authorized User
 
Join Date: Jul 2004
Location: , , .
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!
Reply With Quote
  #4 (permalink)  
Old July 16th, 2004, 11:17 AM
Authorized User
 
Join Date: Jul 2004
Location: , , .
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 *
Reply With Quote
  #5 (permalink)  
Old August 6th, 2004, 08:13 AM
Authorized User
 
Join Date: Jul 2004
Location: , , .
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?

Reply With Quote
  #6 (permalink)  
Old August 6th, 2004, 10:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
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
Reply With Quote
  #7 (permalink)  
Old August 6th, 2004, 11:18 AM
Authorized User
 
Join Date: Jul 2004
Location: , , .
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);

Reply With Quote
  #8 (permalink)  
Old August 7th, 2004, 07:49 AM
Authorized User
 
Join Date: Jul 2004
Location: , , .
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++;
        }
?>
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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



All times are GMT -4. The time now is 05:10 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.