I want to remove the last comma
I want to remove the last comma
am 12.08.2008 21:27:34 von joefazee
------=_Part_49172_7195419.1218569254381
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
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.
------=_Part_49172_7195419.1218569254381--
Re: I want to remove the last comma
am 12.08.2008 21:29:58 von Micah Gersten
Create an Array in the loop and then join with ",\n".
Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com
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.
>
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: I want to remove the last comma
am 12.08.2008 21:36:50 von Yves Sucaet
$out =3D 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
To: "A. Joseph" 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".
Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com
A. Joseph wrote:
> This the script
> $dir =3D "images";
> $d =3D opendir($dir);
> $out =3D "
> var tinyMCEImageList =3D new Array(\n";
> while(false !=3D ($entry =3D readdir($d))) {
> if(preg_match("/(.jpg|.gif|.png)$/", $entry) !=3D false) {
> $out .=3D "['{$entry}', '{$dir}/{$entry}'],\n";
> }
>
> }
> $out .=3D ");\n";
>
> This the out put
>
> var tinyMCEImageList =3D 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 comm=
a,
> 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
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: I want to remove the last comma
am 12.08.2008 22:13:17 von Yves Sucaet
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:
$dir =3D "images";
$d =3D opendir($dir);
$out =3D "
var tinyMCEImageList =3D new Array(\n";
while(false !=3D ($entry =3D readdir($d))) {
if(preg_match("/(.jpg|.gif|.png)$/", $entry) !=3D false) {
$out .=3D "['{$entry}', '{$dir}/{$entry}'],\n";
}
}
$out =3D substr($out, 0, strlen($out) - 2); // MY LINE
$out .=3D ");\n";
hth,
Yves
------ Original Message ------
Received: Tue, 12 Aug 2008 03:08:41 PM CDT
From: "A. Joseph"
To: "YVES SUCAET"
Subject: Re: [PHP-DB] I want to remove the last comma
I tried all this, all i want is something line this
var tinyMCEImageList =3D 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 =3D 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 co=
de
On Tue, Aug 12, 2008 at 12:36 PM, YVES SUCAET wrote=
:
>
> $out =3D 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
> To: "A. Joseph" 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 =3D "images";
> > $d =3D opendir($dir);
> > $out =3D "
> > var tinyMCEImageList =3D new Array(\n";
> > while(false !=3D ($entry =3D readdir($d))) {
> > if(preg_match("/(.jpg|.gif|.png)$/", $entry) !=3D false) {
> > $out .=3D "['{$entry}', '{$dir}/{$entry}'],\n";
> > }
> >
> > }
> > $out .=3D ");\n";
> >
> > This the out put
> >
> > var tinyMCEImageList =3D 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 co=
mma,
> > 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
Re: I want to remove the last comma
am 12.08.2008 22:26:57 von Dee Ayy
What I would do is change
var tinyMCEImageList = new Array(\n";
to have a space and then the newline character at the end like so
var tinyMCEImageList = new Array( \n";
then change your last $out line
$out .= ");\n";
to strip the last 2 characters (which always gets called) and closes
the Array opening parenthesis.
$out = substr($out, 0, -2) . ");\n";
And your Javascript will always be able to be parsed even if there are
no elements in the array.
http://php.he.net/manual/en/function.substr.php
BTW, this is more of a general list question though, not a php-db question.
On Tue, Aug 12, 2008 at 2:27 PM, 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.
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: I want to remove the last comma
am 13.08.2008 00:19:22 von Evert Lammerts
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 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
Re: I want to remove the last comma
am 13.08.2008 03:23:51 von Yi Wang
On Wed, Aug 13, 2008 at 3:27 AM, 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.
>
try rtrim( $out, ', ' ).
--
Regards,
Wang Yi
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php