Re: I want to remove the last comma
| From: | Evert Lammerts | Date: | Tue, 12 Aug 2008 22:19:22 +0000 |
| Subject: | Re: I want to remove the last comma | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-45212@lists.php.net to get a copy of this message | ||
JSON does the trick for you (www.json.org), and PHP's glob() gets rid
of your readdir() overhead (you'll need PHP 5.2 or later for this):
$path = "/path/to/images/";
$images = glob($path . "{*.jpg,*.gif,*.png}", GLOB_BRACE);
for ($i = 0; $i < sizeof($images); $i++)
$images[$i] = array($images[$i], $path . $images[$i]);
$js_output = "var tinyMCEImageList = " . json_encode($images) . ";";
I'd also save myself the overhead of repeating the same path $i times
in my array, and instead just create a JS object:
$path = "/path/to/images/";
$js_output = "var tinyMCEImageList = " . json_encode(array("path" =>
$path, "images" => glob($path . "{*.jpg,*.gif,*.png}", GLOB_BRACE))) .
";";
You can append the path client side after he page is parsed.
Apart from that, echo-ing javascript in your php code is never really
a good idea when it comes to louse coupling of your web applications.
If that's no consideration for you - party on.
Again, this is not a PHP-DB question, you should subscribe to the
PHP-GENERAL (or-whatever-it's-called) list.
On Tue, Aug 12, 2008 at 9:27 PM, A. Joseph <joefazee@gmail.com> 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.
>