0

PHP

$meta = get_post_custom($post->ID); 
$images = $meta['img1'][0];    // urls separated with comma
$images = explode(',', $images);
foreach ($images as $image) {
    echo '{image : '.$image.', title : "Credit: x"},'; 
};

Output:

{image : 'http://localhost/slideshow/1.jpg', title : 'Credit: x'},
{image : 'http://localhost/slideshow/2.jpg', title : 'Credit: x'},
{image : 'http://localhost/slideshow/3.jpg', title : 'Credit: x'},
{image : 'http://localhost/slideshow/4.jpg', title : 'Credit: x'}, // last comma, just after }

I would like to remove the last commented comma in output.

This is exactly what I'm trying to get:

Desired output:

{image : 'http://localhost/slideshow/1.jpg', title : 'Credit: x'},
{image : 'http://localhost/slideshow/2.jpg', title : 'Credit: x'},
{image : 'http://localhost/slideshow/3.jpg', title : 'Credit: x'},
{image : 'http://localhost/slideshow/4.jpg', title : 'Credit: x'}
1
  • 2
    How about using the build in PHP functions to generate valid JSON? (json_encode) Commented Nov 26, 2013 at 6:38

4 Answers 4

2

You have lots of options:

// Using rtrim
$meta = get_post_custom($post->ID); 
$images = $meta['img1'][0];    // urls separated with comma
$images = explode(',', $images);
$string = '';
foreach ($images as $image) {
    $string .= '{image : '.$image.', title : "Credit: x"},'; 
};
$string = rtrim($string, ',');
echo $string;

// Using substring
$meta = get_post_custom($post->ID); 
$images = $meta['img1'][0];    // urls separated with comma
$images = explode(',', $images);
$string = '';
foreach ($images as $image) {
    $string .= '{image : '.$image.', title : "Credit: x"},'; 
};
$string = substr($string, 0, -1);
echo $string;

// Using implode
$meta = get_post_custom($post->ID); 
$images = $meta['img1'][0];    // urls separated with comma
$images = explode(',', $images);
$stringElements = array();
foreach ($images as $image) {
    stringElements[] = '{image : '.$image.', title : "Credit: x"}'; 
};

$string =  implode(',', $stringElements);
echo $string;

Also consider using a more efficient way to create JSON strings: json_encode.

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

Comments

2

You can buffer output into variable and solve this way:

echo rtrim($bufferedData, ',');

But as I can see, it's better for you to use json functions.

Comments

2

So all your code is about creating JSON from your data. That will be:

$data   = 'http://localhost/slideshow/1.jpg,http://localhost/slideshow/2.jpg,http://localhost/slideshow/3.jpg';
$credit = 'x';
$result = json_encode(array_map(function($image) use ($credit)
{
   return ['image'=>$image, 'Credit'=>$credit];
}, explode(',', $data)));
//var_dump($result);

Comments

1

Maybe this using rtrim()

$images = 'image1.png,image2.png,image3.png';
$ex = explode(',', $images);
foreach ($ex as $image) {
    $image_string .= "{'image' : '{$image}', 'title' : 'Credit: x'},"; 
}

print rtrim($image_string, ',');

Above returns below

{'image' : 'image1.png', 'title' : 'Credit: x'},
{'image' : 'image2.png', 'title' : 'Credit: x'},
{'image' : 'image3.png', 'title' : 'Credit: x'}

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.