homepage Welcome to WebmasterWorld Guest from 54.161.155.142
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Finding a sequence of numbers in a string
Crump




msg:3933556
 2:52 pm on Jun 15, 2009 (gmt 0)

I have a string that will contain a sequence of consecutive numbers at some point. For instance

390 joe smith 1923892 baltimore, md

I need to extract the 0923892 out of this string. I can't use the spaces as a method of finding where it is. How can I extract the 7 digit number?

 

jatar_k




msg:3933600
 3:41 pm on Jun 15, 2009 (gmt 0)

you need to find some reliable pattern to pull it out

where does the data come from? It must be generated with some consistency if it comes from a single source, if it is from multiple sources then you may need a pattern per source.

once you can identify the pattern then you can make decisions on how to best extract it

nalin




msg:3933629
 4:36 pm on Jun 15, 2009 (gmt 0)

use a regular expression

ie

preg_match('/[0-9]{7}/',$string,$matches);
var_dump($matches);

jatar_k




msg:3933665
 5:36 pm on Jun 15, 2009 (gmt 0)

that does assume though that the desired 7 digit number is the only 7 digit number that will ever occur in the data, which may not be the case

g1smd




msg:3933670
 5:40 pm on Jun 15, 2009 (gmt 0)

Is the number always directly preceded and followed by spaces and/or punctuation?

NomikOS




msg:3933692
 5:56 pm on Jun 15, 2009 (gmt 0)

first approah:
<?php 
$str = '1234567 str 999999999586 390 joe smith popopopopopopopopo ';
$str .= '1923892 baltimore, md 787878 1234568 8586552895 1234569';

if (preg_match_all("/[^\d]+(\d{7})[^\d]+/si", $str, $extract, PREG_SET_ORDER))
{
foreach ($extract as $number)
{
$extratctedNumbers[] = $number[1];
}
}

var_dump($str);
die(var_dump($extratctedNumbers));
?>

but. this not extract numbers in the extrems!

NomikOS




msg:3933699
 6:03 pm on Jun 15, 2009 (gmt 0)

Quick && dirty solution:

<?php 
$str = '1234567 str 999999999586 390 joe smith popopopopopopopopo ';
$str .= '1923892 baltimore, md 787878 1234568 8586552895 1234569';

if (preg_match_all("/(\d+)/si", $str, $extract, PREG_SET_ORDER))
{
foreach ($extract as $number)
{
if (strlen($number[1]) == 7)
$extratctedNumbers[] = $number[1];
}
}

var_dump($str);
die(var_dump($extratctedNumbers));
?>

this is OK I think...
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved