Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

Following is the String that holds the contact info. This string is dynamic i.e. sometimes new fields eg: mobile number may add up or old fields say: tel number may delete.

                              <?php $str = 
                                "tel: (123) 123-4567
                                fax : (234) 127-1234
                                email : [email protected]";
                                $newStr =  explode(':', $str);
                                echo '<pre>'; print_r($newStr); 
                              ?>

Output of the code is:

                        Array
                            (
                                [0] => tel
                                [1] =>  (123) 123-4567
                                                                fax 
                                [2] =>  (234) 127-1234
                                                                email 
                                [3] =>  [email protected]
                            )

But the output needed is in the following format:

                        Array
                            (
                                [tel] => (123) 123-4567
                                [fax] =>  (234) 127-1234            
                                [email] =>  [email protected]
                            )

I tried exploding it in may ways... but didn't work. please guide.

share|improve this question
    
you are exploding with delimiter :. So between (123) 123-4567 and fax there is no separator like :. So you are getting both of them in the same value. –  Pankit Kapadia Dec 11 '12 at 6:47
    
@PankitKapadia What delimiter shd i use? I didn't find any other meaningful separator. –  user1871640 Dec 11 '12 at 6:49
1  
Is there a newline between each set of values in the text? If so, use "\n" –  Anton Dec 11 '12 at 6:49
    
@user1871640 - delimiter you used is not a problem. You can use any. I just want to say that you are not getting separate values because there is no delimiter between (123) 123-4567 and fax –  Pankit Kapadia Dec 11 '12 at 6:51
    
@user1871640 - check the UPVOTED answer !! –  Pankit Kapadia Dec 11 '12 at 6:53

6 Answers 6

up vote 6 down vote accepted
$txt = 
                            "tel: (123) 123-4567
                            fax : (234) 127-1234
                            email : [email protected]";
$arr = array();
$lines = explode("\n",$txt);
foreach($lines as $line){
    $keys = explode(":",$line);
    $key = trim($keys[0]);
    $item = trim($keys[1]);
    $arr[$key] = $item;
}
print_r($arr);

CodePade

share|improve this answer
1  
Thanks @NullPointer. –  Taha Paksu Dec 11 '12 at 6:52
foreach( $newStr as $key=>$value){
      echo $key;
      echo $value;
} 
share|improve this answer
    
OP does not wants to just echo(show) he wants to just echo ... check tpaksu –  NullPoiиteя Dec 11 '12 at 6:55

Here's a shorter way with regular expressions.

preg_match_all('/(\w+)\s*:\s*(.*)/', $str, $matches);
$newStr = array_combine($matches[1], $matches[2]);

print_r($newStr);

Results:

Array
(
    [tel] => (123) 123-4567
    [fax] => (234) 127-1234
    [email] => [email protected]
)

example here

This example assumes, however, that each data pair is on a separate line as in your provided string and that the "key" contains no spaces.

share|improve this answer
$str =
    "tel: (123) 123-4567
    fax : (234) 127-1234
    email : [email protected]";

$array = array();
foreach (preg_split('~([\r]?[\n])~', $str) as $row)
{
    $rowItems = explode(':', $row);
    if (count($rowItems) === 2)
        $array[trim($rowItems[0])] = trim($rowItems[1]);
}

You have to use preg_split because there could be different line-ending on each system. There is also possibility that the string is invalid so you should handle that (condition in the foreach cycle)

share|improve this answer

use preg_split with the delimeter ":" and "\n" (newline character):

$newStr = preg_split("\n|:", $str);
share|improve this answer
<?php
    $str = 
    "tel: (123) 123-4567
    fax : (234) 127-1234
    email : [email protected]";

$contacts = array();
$rows = explode("\n", $str);
foreach($rows as $row) {
    list($type, $val) = explode(':', $row);
    $contacts[trim($type)] = trim($val);
}
var_export($contacts);

returns

array (
  'tel' => '(123) 123-4567',
  'fax' => '(234) 127-1234',
  'email' => '[email protected]',
)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.