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
user2864740
  • 60,010
  • 15
  • 145
  • 220
Dows
  • 57
  • 6

8 Answers8

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'
Rizier123
  • 58,877
  • 16
  • 101
  • 156
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.

mario
  • 144,265
  • 20
  • 237
  • 291
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

Starfish
  • 3,344
  • 1
  • 19
  • 47
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.

RhapX
  • 1,663
  • 2
  • 10
  • 17
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

Community
  • 1
  • 1
JasonKretzer
  • 192
  • 1
  • 11
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.

Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
1

Hello you can use Implode.

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

the output will be:

'image1','image2','image3'
snippster
  • 98
  • 10
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);
Alex Belyaev
  • 1,417
  • 1
  • 11
  • 15