Re: I want to remove the last comma

From: Date: Tue, 12 Aug 2008 20:13:17 +0000
Subject: Re: I want to remove the last comma
Groups: php.db 
Request: Send a blank email to php-db+get-45210@lists.php.net to get a copy of this message
Ah, I forgot the \n newline character you're adding as well. That would require you to chop off two character instead of one :-) Here's the adjusted script: <snip> $dir = "images"; $d = opendir($dir); $out = " var tinyMCEImageList = new Array(\n"; while(false != ($entry = readdir($d))) { if(preg_match("/(.jpg|.gif|.png)$/", $entry) != false) { $out .= "['{$entry}', '{$dir}/{$entry}'],\n"; } } $out = substr($out, 0, strlen($out) - 2); // MY LINE $out .= ");\n"; </snip> hth, Yves ------ Original Message ------ Received: Tue, 12 Aug 2008 03:08:41 PM CDT From: "A. Joseph" <joefazee@gmail.com> To: "YVES SUCAET" <yves.sucaet@usa.net> Subject: Re: [PHP-DB] I want to remove the last comma I tried all this, all i want is something line this var tinyMCEImageList = new Array( ['1_h_1.gif', 'images/1_h_1.gif'], ['1_h_1.gif', 'images/1_h_1.gif'] ); But this what i got var tinyMCEImageList = new Array( ['1_h_1.gif', 'images/1_h_1.gif'], ['1_h_1.gif', 'images/1_h_1.gif'], ); Remember the last commad on the last element. please show some example code On Tue, Aug 12, 2008 at 12:36 PM, YVES SUCAET <yves.sucaet@usa.net> wrote: > > $out = substr($out, 0, strlen($out) - 1); > > Removes the overhead cost of creating the array and then joining it. > > HTH, > > Yves > > ------ Original Message ------ > Received: Tue, 12 Aug 2008 02:31:28 PM CDT > From: Micah Gersten <micah@onshore.com> > To: "A. Joseph" <joefazee@gmail.com>Cc: php-db@lists.php.net > Subject: Re: [PHP-DB] I want to remove the last comma > > Create an Array in the loop and then join with ",\n". > > A. Joseph wrote: > > This the script > > $dir = "images"; > > $d = opendir($dir); > > $out = " > > var tinyMCEImageList = new Array(\n"; > > while(false != ($entry = readdir($d))) { > > if(preg_match("/(.jpg|.gif|.png)$/", $entry) != false) { > > $out .= "['{$entry}', '{$dir}/{$entry}'],\n"; > > } > > > > } > > $out .= ");\n"; > > > > This the out put > > > > var tinyMCEImageList = new Array( > > ['1_h_1.gif', 'images/1_h_1.gif'], > > ['1_h_2.gif', 'images/1_h_2.gif'], > > ['rss_2.jpg', 'images/rss_2.jpg'], > > ['spacer.gif', 'images/spacer.gif'], # I want to remove this comma > > ); > > > > if the last element (['spacer.gif', 'images/spacer.gif'],) contain > > comma, > > javascript will return error, so i want to remove it. > >

« previous php.db (#45210) next »