0

I am trying to remove last character in the output of this code:

$images = "image1,image2,image3";
$images = explode(',', $images);

foreach ($images as $image) {
    echo "'$image',";
}}

current output:

'image1','image2','image3',
                        //^ See here

expected output:

'image1','image2','image3'
                        //^ See here
2
  • Sorry i updated @Script47 Commented Aug 8, 2015 at 21:38
  • 1
    Looks like a frail attempt at building sort of JSON or a CSV list. Commented Aug 8, 2015 at 21:41

8 Answers 8

4

Just keep it simple and use implode(), e.g.

<?php

    $images = "image1,image2,image3";
    $arr = explode(",", $images);
    echo "'" . implode("','", $arr) . "'";

?>

output:

'image1','image2','image3'
Sign up to request clarification or add additional context in comments.

3 Comments

Personally I think rtrim would be simpler, but each to their own.
Thanks great.. @Script47
For edit my question and thanks to @Rizier123
2

Looks like we don't have enough answers yet. So here's another trivial approach.

If there's a comma-separated list already, and it just needs quoting, then a silly regex suffices:

$images = "image1,image2,image3";
echo preg_replace("~[^,]+~",    "'$0'",   $images);
#                     ↑           ↑
#           anything but commas   ↑
#                            wrap in quotes

Which avoids the need to manually loop over things, or postprocess the result.

2 Comments

I'm missing some unicode arrows in your answer :]
Oh. You're right. That was highly neglient. :↑
1

This will remove the trailing comma from the $string.

rtrim($string, ",") would remove trailing commas.

I don't know how to mark this as a duplicate, but here is another that was also marked as a duplicate(Remove the last character from string). answer is from the link I put above

1 Comment

The flag link underneath the questions tags, then follow the instructions. I did this one.
1

rtrim($string, ",") would remove trailing commas. rtrim

and

trim($string, ",") would remove trailing and leading commas. trim

Looking at your code it would be useful in your case to use implode

implode(",", $images)

Implode does not put a comma at the end, only between variables

2 Comments

How? i don't understand.Please add example.. Thanks
Remove the foreach loop. Just add this: echo(implode(",", $images));
1

To remove the last character of a string you can just cut it off using substr():

$text = "image,";
echo substr($text, 0, strlen($text) - 1); // "image"

It reads everything from the zeroeth character up to the last character minus one (i.e. omitting the last character).

If you know what the last character is, such as in image, (the comma), then you can just trim it:

$text = "image,";
echo rtrim($text, ','); // "image"

That will remove all , characters from the end of the string.

Comments

1

Hello you can use Implode.

$images = "image1,image2,image3";
$images = explode(',', $images);
echo "'".implode("','",$images)."'";

the output will be:

'image1','image2','image3'

Comments

1

Option 1

When you are looping through the data, add it to a string and then use PHP's rtrim function to remove the ending comma.

$images = "image1,image2,image3";
$images = explode(',', $images);

foreach ($images as $image) {
    $string .= "'$image',";
}

echo rtrim($string, ',');

Option 2

If you really wanted to get into it, you could utilize regular expressions and PHP preg_replace:

$images       = "image1,image2,image3";
$patterns     = ['/(^)/', '/($)/', '/,/'];
$replacements = ["'", "'", "','"];

echo preg_replace($patterns, $replacements, $images);

This would eliminate the need for looping, imploding, exploding, etc.

Comments

1

Let me suggest another solution. In my opinion it will be a little clearer:

$images = "image1,image2,image3";

$quotified = array_map(
                function($x) {
                    // Add quotes... You can also add trim() here 
                    // ...or do some another 'purifying' things
                    // ...that will be applied to each image list element
                    return "'$x'"; 
                }, 
                explode(',', $images)
            );

$images = implode(",", $quotified);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.