kevinkhan Posted April 13, 2010 Share Posted April 13, 2010 i want to extract numbers in these formats 08x xxxxxxx 08xxxxxxxx (08x)xxxxxxx (08x) xxxxxxx 08x-xxxxxxx 08x - xxxxxxx from the following string Trooper, 3.1 Itr 06 A3, 3dr, 1.6, 82,000 kim, good condition, taxed, priced white, tax 04-10, DOE 1 owner, full service history, to sell. flOOD. Tel. 10-10. €1250. Tel. 2yr net 06/10, blacl< in col- 085-7466081 087-9911143 our, immaculate condition. €1750. Tel. 086-6814743. 05 Nissan Navara, crew cab, 99 Isuzu 3.5 turbo, pick up, grey, new model, 70k miles, full test 99k,.€1800 ono. Tel.. 07 M Estate, all usual. taxed and MOT 2011, new 087-2084528 extras, €21 K. 086-8281633 tyres and many extras. POA. Tel 087-7609868 Opel Frontera swb for parts, 93 Cabriolet, 2.3L, 146k '06 Kia Rio 1.4, 4dr '02 Skoda Octavia 1.4 new diesel pump recently miles, manual roof, NCT '05 Corolla dsll.4, 5dr '02 Honda Civic 3dr 1.4 05 Pathfinder, 2.5 DCi, auto, fitted, blue, 2002, 75k miles, 11-11, 18" alloys, good tyres silver, 7 seater, all leather, Engine & gearbox perfect CD player, fully serviced '05 Ford Focus 1.4, 4dr '02 VW Bora 1.4 electric & heated front seats, €800 Tel or Text. including tbelt and brakes all bluetooth, sat nav, reversing 0868317232 round. THis is the code i have so far if (preg_match_all('/(08[0-9\- ]{8,})/', $data, $match)) { print_r($match[0]); } for the first array i am getting this output Array ( [0] => 085-7466081 087-9911143 [1] => 086-6814743 [2] => 087-2084528 [3] => 086-8281633 [4] => 087-7609868 [5] => 0868317232 [6] => 086-1036878 [7] => 087-7920104 [8] => 087-6340434 [9] => 087-2118729 [10] => 086-8681386 128 [11] => 087-6764404 [12] => 086-8706992 [13] => 086-8461120 [14] => 087-9719520 96 [15] => 087-9145559 [16] => 087-9035224 085-1221027 ) how come in the first array 0 i am getting two numbers as appose to one number??? Ill also want to strip all the numbers so that they only consist of 10 digits and no - or ( ) symbols.. Does anybody know what i am doing wrong? is there something wrong with my regex?? Quote Link to comment https://forums.phpfreaks.com/topic/198396-need-a-simple-regex/ Share on other sites More sharing options...
Ken2k7 Posted April 13, 2010 Share Posted April 13, 2010 The {8,} is messing it up because that means match a minimum of 8 times. Try: preg_match_all('#08\d[ \-]?\d+#', $data, $match) Quote Link to comment https://forums.phpfreaks.com/topic/198396-need-a-simple-regex/#findComment-1041039 Share on other sites More sharing options...
premiso Posted April 13, 2010 Share Posted April 13, 2010 <?php $string = "asdfasdf asdf asdf asdf asdf asdf 085-7466081 087-9911143 asdfasdf asdf asdf asdf asdf asdf 086-6814743 asdfasdf asdf asdf asdf asdf asdf (087)-2084528 asdfasdf asdf asdf asdf asdf asdf 086 8281633"; if (preg_match_all('~[(]{0,1}[0-9]{3}[- _]{0,1}[0-9]{7}~', $string, $matches)) { $number = array(); foreach ($matches as $match) { $number[] = preg_replace('~[^0-9]~', '', $match); } print_r($number); } ?> Should output: Array ( [0] => Array ( [0] => 0857466081 [1] => 0879911143 [2] => 0866814743 [3] => 0868281633 ) ) The regex can probably be done better / more efficient but there was my attempt at it EDIT: Posted after Ken's as mine removes the - ( and spaces as well. Quote Link to comment https://forums.phpfreaks.com/topic/198396-need-a-simple-regex/#findComment-1041045 Share on other sites More sharing options...
kevinkhan Posted April 13, 2010 Author Share Posted April 13, 2010 <?php $string = "asdfasdf asdf asdf asdf asdf asdf 085-7466081 087-9911143 asdfasdf asdf asdf asdf asdf asdf 086-6814743 asdfasdf asdf asdf asdf asdf asdf (087)-2084528 asdfasdf asdf asdf asdf asdf asdf 086 8281633"; if (preg_match_all('~[(]{0,1}[0-9]{3}[- _]{0,1}[0-9]{7}~', $string, $matches)) { $number = array(); foreach ($matches as $match) { $number[] = preg_replace('~[^0-9]~', '', $match); } print_r($number); } ?> Should output: Array ( [0] => Array ( [0] => 0857466081 [1] => 0879911143 [2] => 0866814743 [3] => 0868281633 ) ) The regex can probably be done better / more efficient but there was my attempt at it EDIT: Posted after Ken's as mine removes the - ( and spaces as well. im afraid your regexs dont allow for numbers in this format (08x)xxxxxxx or (08x) xxxxxxx can somebody please adjust the obove thanks Quote Link to comment https://forums.phpfreaks.com/topic/198396-need-a-simple-regex/#findComment-1041050 Share on other sites More sharing options...
premiso Posted April 13, 2010 Share Posted April 13, 2010 if (preg_match_all('~[(]{0,1}[0-9]{3}[)- _]{0,1}[0-9]{7}~', $string, $matches)) { Should resolve that. Quote Link to comment https://forums.phpfreaks.com/topic/198396-need-a-simple-regex/#findComment-1041071 Share on other sites More sharing options...
cags Posted April 13, 2010 Share Posted April 13, 2010 Not tested, but off the top of my head something like this should work... $pattern = '#\(?08[0-9][ )-]{0,3}[0-9]{7}#'; Quote Link to comment https://forums.phpfreaks.com/topic/198396-need-a-simple-regex/#findComment-1041085 Share on other sites More sharing options...
kevinkhan Posted April 13, 2010 Author Share Posted April 13, 2010 Not tested, but off the top of my head something like this should work... $pattern = '#\(?08[0-9][ )-]{0,3}[0-9]{7}#'; Thanks this one worked perfectly Quote Link to comment https://forums.phpfreaks.com/topic/198396-need-a-simple-regex/#findComment-1041171 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.