3

I have a php variable that contain value of textarea as below.

Name:Jay
Email:[email protected]
Contact:9876541230

Now I want this lines to in array as below.

Array
(
[Name] =>Jay
[Email] =>[email protected]
[Contact] =>9876541230
)

I tried below,but won't worked:-

$test=explode("<br />", $text); 
print_r($test);
6
  • You can split() the string to separate by \n\r (jumb line) and : to prop/value Commented Aug 17, 2017 at 10:15
  • I have try this Commented Aug 17, 2017 at 10:16
  • $test=explode("<br />", $text); print_r($test); but each line become an key value but i want as mention in question. Commented Aug 17, 2017 at 10:16
  • <br/> tag is html line break .it will not consider as new line php server side . Commented Aug 17, 2017 at 10:19
  • refer to this url sitepoint.com/community/t/… Commented Aug 17, 2017 at 10:27

4 Answers 4

6

you can try this code using php built in PHP_EOL but there is little problem about array index so i am fixed it

<?php
$text = 'Name:Jay
Email:[email protected]
Contact:9876541230';
$array_data = explode(PHP_EOL, $text);

$final_data = array();
foreach ($array_data as $data){
    $format_data = explode(':',$data);
    $final_data[trim($format_data[0])] = trim($format_data[1]);
}
echo "<pre>";
print_r($final_data);

and output is :

Array
(
    [Name] => Jay
    [Email] => [email protected]
    [Contact] => 9876541230
)
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your answer but see my question.I want array as given i question
ShafiqulIslam and @Sanj you both have to take care of extra spaces if some-one added accidently in text-area. You array indexes and value both have spaces then (if you are using above answer)
@ShafiqulIslam i have solution of that in my answer.If you want to check
5

Easiest way to do :-

$textarea_array = array_map('trim',explode("\n", $textarea_value)); // to remove extra spaces from each value of array
print_r($textarea_array);

$final_array = array();
foreach($textarea_array as $textarea_arr){
    $exploded_array = explode(':',$textarea_arr);
    $final_array[trim($exploded_array[0])] = trim($exploded_array[1]);

}

print_r($final_array);

Output:- https://eval.in/846556

Comments

0

This also works for me.

$convert_to_array = explode('<br/>', $my_string); 
for($i=0; $i < count($convert_to_array ); $i++)
{ 
$key_value = explode(':', $convert_to_array [$i]); 
$end_array[$key_value [0]] = $key_value [1];
} 
print_r($end_array); ?>

Comments

0

I think you need create 3 input:text, and parse them, because if you write down this value in text area can make a mistake, when write key. Otherwise split a string into an array, and after create new array where key will be odd value and the values will be even values old array

Comments

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.