regex phones and phones type

Discussion in 'PHP' started by bumbar, Jan 28, 2014.

  1. #1
    Hello,

    I have this string from db:

    $str = "1:78 78 12345, 2:678 77 1234567899, 1:44 4 123456789";

    The first is the type of phone - 1 or 2, after : should phone - 1: 2: 1: 1: . All separated by commas.
    How can I split this string so it foreach it.
    After each comma should type and next is phone.

    Thanks!
     
    bumbar, Jan 28, 2014 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    not sure if I understood correctly. try this

    $str = "1:78 78 12345, 2:678 77 1234567899, 1:44 4 123456789";
    
    $str_ar = explode(',',$str);
    
    foreach ($str_ar as $item ) {
        $parts = explode (':',$item);
    
        echo 'part 1: '.$parts[0].'<br />;
         echo 'part 2: '.$parts[1].'<br /><br /><br />';
    
    }
    PHP:
     
    stephan2307, Jan 29, 2014 IP