Re: I want to remove the last comma

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx> 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.
>

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux