0

I get a variable which looks like this:

$var = "User list 1,,User list 3,User list 4,,,,User list 5,,,User list 6"

My question is how can I display that like below:

User list 1
User list 3
User list 4
User list 5
User list 6

I tried this: echo str_replace(",","<br>",$var);

but I get the wrong output:

    User list 1

    User list 3
    User list 4



    User list 5


    User list 6

Any help? Thanks

1
  • Your have back to back , in your string that's the problem here. Commented Sep 28, 2015 at 5:19

4 Answers 4

3

Simply use implode and explode along with array_filter like as

$var = "User list 1,,User list 3,User list 4,,,,User list 5,,,User list 6";

echo implode("<br>",array_filter(explode(',',$var)));

Output :

User list 1
User list 3
User list 4
User list 5
User list 6

Edited :

Lets make it more clear stepwise

Step 1

We'll exploding string at , using explode function of PHP like as

explode(',',$var);

which'll result into

Array
(
    [0] => User list 1
    [1] => 
    [2] => User list 3
    [3] => User list 4
    [4] => 
    [5] => 
    [6] => 
    [7] => User list 5
    [8] => 
    [9] => 
    [10] => User list 6
)

Now we've empty values within array. So we need to remove that empty values using array_filter like as

array_filter(explode(',',$var));

results into an array

Array
(
    [0] => User list 1
    [2] => User list 3
    [3] => User list 4
    [7] => User list 5
    [10] => User list 6
)

Now as OP stated output must break at new line we'll simply use implode along with <br> like as

echo implode("<br>",array_filter(explode(',',$var)));

Results

User list 1
User list 3
User list 4
User list 5
User list 6

Demo

Explanation : Your attempt

echo str_replace(",","<br>",$var);

What you were doing over here is replacing each ,(comma) along with <br> which'll simply output your result.

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

Comments

2

You can try this. First replace consecutive spaces with single space and then explode on a single comma and then implode the array on <br> tag to form the desired list as follows.

$string="User list 1,,User list 3,User list 4,,,,User list 5,,,User list 6";
$string = preg_replace("/,+/", ",", $string);
echo (implode('<br>',explode(',',$string)));

Comments

1

You can make use of explode

$var = "User list 1,,User list 3,User list 4,,,,User list 5,,,User list 6";

$a = explode(',',$var);

foreach($a as $b=>$c)
{
    if(!empty($c))
          echo $c."<br>";
}

Comments

1

You can use this

        $var = "User list 1,,User list 3,User list 4,,,,User list 5,,,User list 6";
        $arrvar = explode(',',$var);
        $arrvar = array_values(array_filter($arrvar));
        foreach($arrvar as $key=>$val)
        {

           echo $val."<br>";
        }

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.