0

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.

5
  • 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. Commented Dec 11, 2012 at 6:47
  • @PankitKapadia What delimiter shd i use? I didn't find any other meaningful separator. Commented Dec 11, 2012 at 6:49
  • 1
    Is there a newline between each set of values in the text? If so, use "\n" Commented Dec 11, 2012 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 Commented Dec 11, 2012 at 6:51
  • @user1871640 - check the UPVOTED answer !! Commented Dec 11, 2012 at 6:53

6 Answers 6

6
$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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0
<?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]',
)

Comments

0

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

$newStr = preg_split("\n|:", $str);

Comments

0
$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)

Comments

0
foreach( $newStr as $key=>$value){
      echo $key;
      echo $value;
} 

1 Comment

OP does not wants to just echo(show) he wants to just echo ... check tpaksu

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.