0

I have some strings which contain , character and I need to explode them into array. For example there are two cases

    $str = 'Jack, Rose, John';

Or

    $str = 'Jack';

When I used this code to each case

    $formatted = str_replace(' ', '', $str); // Remove spaces
    $names = explode(',',$formatted); // Split strings to array

In first case, everything goes fine. Three name (Jack, Rose, John) are splitted into array that I can use.

    $str = 'Jack, Rose, John';
    $formatted = str_replace(' ', '', $str);
    $names = explode(',',$formatted);
    foreach($names AS $name)
    {
            // Get user from database using $name.
            // Everything works just fine.
    }

But in the second case I didn't get an array. When I used foreach it just return empty string for each loop.

    $str = 'Jack';
    $formatted = str_replace(' ', '', $str);
    $names = explode(',',$formatted);
    foreach($names AS $name)
    {
            // Get user from database using $name.
            // Return error because $name empty ?!
    }

I appreciate your answers to tell me why this happened.

5
  • 1
    what is wrong ? - sandbox.onlinephpfunctions.com/code/… Commented Jul 30, 2019 at 8:45
  • Code seems to work. Commented Jul 30, 2019 at 8:46
  • 1
    'jack' Does not have a comma so this is expected behavior. Commented Jul 30, 2019 at 8:47
  • it works in all case. i didn't get any error in your code. Commented Jul 30, 2019 at 8:55
  • 1
    Note that by doing $formatted = str_replace(' ', '', $str); you also remove spaces in the names. So Van Gogh would become VanGogh. Commented Jul 30, 2019 at 9:07

2 Answers 2

2

Test code :

$str1 = 'Jack, Rose, John';
$str2 = 'Jack';

$formatted1 = str_replace(' ', '', $str1);
$names1 = explode(',', $formatted1);

$formatted2 = str_replace(' ', '', $str2);
$names2 = explode(',', $formatted2);

foreach($names1 as $name){echo $name . "<br/>";}
foreach($names2 as $name){echo $name . "<br/>";}

Output :

Jack
Rose
John
Jack

This is how I've tested your code and it seems to work fine. If it still doesn't work fine for you, try adding a comma at the end of your string. Though it will add an empty array entry at the end of your array so I'd recommend using array_pop before your foreach.

Here's an example :

$str = 'Jack, Rose, John';
$str .= ','; //Adding a comma

$formatted = str_replace(' ', '', $str);
$names = explode(',', $formatted);
array_pop($names); //Popping the last empty entry

foreach($names as $name){
    //Do your thing here
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, add a comma to the last and work perfectly!
0

There is no need to used str_replace method. explode(',', $str) will help to form array from giving string, because it store data in array form even for single value.
$str = 'Jack, Rose, John'; $names = explode(',', $str); var_dump($names);

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.