Jump to content

Need a simple Regex


kevinkhan

Recommended Posts

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??

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.