0

I want to remove the last record comma from fetched value. I used $album1 = substr($album,0,-1); to remove last character comma from the value `$ album'. I caught:

SyntaxError: Unexpected token ,'

Whats wrong with this in substr()?

$album = {"media_type":"image/png","pic_id":"zhadb"},
         {"media_type":"image/png","pic_id":"zhadb"},
         {"media_type":"image/video","pic_id":"kg5k4"},

while($fet_pic=mysql_fetch_array($albpic)) {  
    $album.=$fet_pic['CONTENT_VALUE'].',';
}

$album1 = substr($album,0,-1);
user3386779
  • 6,883
  • 20
  • 66
  • 134

3 Answers3

1

use PHP rtrim Strip whitespace (or other characters) from the end of a string

rtrim($album, ",")

Full code

<?php 
$album = '{"media_type":"image/png","pic_id":"zhadb"},
         {"media_type":"image/png","pic_id":"zhadb"},
         {"media_type":"image/video","pic_id":"kg5k4"},';

         echo rtrim($album, ",");

Demo

Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20
0

you can uses trim to remove last comma from the string

like this

$album1 = trim($album, ',');
Snehal S
  • 865
  • 4
  • 13
0

Try with following code :

$rows = array();
while($fet_pic=mysql_fetch_array($albpic))
{  
    $rows[] = $fet_pic['CONTENT_VALUE'];
}
$album = implod(", ",$rows);
ranakrunal9
  • 13,320
  • 3
  • 42
  • 43