php-general Digest 26 Jan 2007 05:02:43 -0000 Issue 4590

Topics (messages 247779 through 247806):

Re: sortind arrays
        247779 by: Ford, Mike
        247782 by: Alexander Sagen

php from address
        247780 by: Chantal Rosmuller
        247786 by: M.Sokolewicz
        247787 by: M.Sokolewicz

Re: most powerful php editor
        247781 by: Børge Holen
        247803 by: Richard Lynch
        247804 by: Richard Lynch

Parsing AJAX post data -- The Way
        247783 by: Myron Turner
        247789 by: M5
        247800 by: Richard Lynch
        247806 by: M5

Re: preg_match problem
        247784 by: Beauford
        247785 by: Jim Lucas
        247799 by: Richard Lynch

Re: Send Email to Mobiles
        247788 by: Youri LACAN-BARTLEY

bit wise math? Is there a function to easily return the bits?
        247790 by: blackwater dev
        247791 by: tg-php.gryffyndevelopment.com
        247792 by: Jon Anderson
        247794 by: Roman Neuhauser
        247795 by: Jim Lucas
        247797 by: Paul Novitski
        247798 by: Richard Lynch

retrieve all the groups a user is memberOf from active directory?
        247793 by: Bing Du
        247796 by: Richard Lynch

Re: Validating a link in php
        247801 by: Richard Lynch

Re: JPEG info needed
        247802 by: Richard Lynch
        247805 by: Gerry Danen

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        php-general@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On 25 January 2007 10:55, Alexander Sagen wrote:

> Roman Neuhauser skrev:
> > # [EMAIL PROTECTED] / 2007-01-25 08:12:14 +0200:
> > > How can I sort an array like this so that it would be ASC ordered
> > > by the [1] key in subarrays? I need to maintain only the subarray
> > > key - value pairs. (Do I make sense?) 
> > > 
> > > Array
> > > (
> > >     [0] => Array
> > >         (
> > >             [0] => Logo
> > >             [1] => NameC
> > >             [2] => Home
> > >             [3] => url
> > >         )
> > > 
> > >     [1] => Array
> > >         (
> > >             [0] => Logo
> > >             [1] => NameA
> > >             [2] => Home
> > >             [3] => url
> > >         )
> > > 
> > >     [2] => Array
> > >         (
> > >             [0] => Logo
> > >             [1] => NameG
> > >             [2] => Home
> > >             [3] => url
> > >         )
> > > }
> > 
> > http://www.php.net/usort
> > 
> I think usort would be a bit overkill, he would probably find himself
> making a function to sort and maintain the association between the
> subarrays. Go with array_multisort, it would be a one-liner.

What total tosh!  array_multisort() won't handle this one -- usort() is 
correct.  The only function needed is a (one-liner!) custom comparison to 
compare individual [1] elements -- usort() takes care of all the rest:

   function compare_1($a, $b) { return strcmp($a[1], $b[1]); }

   usort($array, 'compare_1');

Or even, for single use, collapse it to:

   usort($array, create_function('$a,$b', 'return strcmp($a[1], $b[1]);');

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 



To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

--- End Message ---
--- Begin Message ---
Ford, Mike skrev:
On 25 January 2007 10:55, Alexander Sagen wrote:

Roman Neuhauser skrev:
# [EMAIL PROTECTED] / 2007-01-25 08:12:14 +0200:
How can I sort an array like this so that it would be ASC ordered
by the [1] key in subarrays? I need to maintain only the subarray
key - value pairs. (Do I make sense?)
Array
(
    [0] => Array
        (
            [0] => Logo
            [1] => NameC
            [2] => Home
            [3] => url
        )

    [1] => Array
        (
            [0] => Logo
            [1] => NameA
            [2] => Home
            [3] => url
        )

    [2] => Array
        (
            [0] => Logo
            [1] => NameG
            [2] => Home
            [3] => url
        )
}
http://www.php.net/usort

I think usort would be a bit overkill, he would probably find himself
making a function to sort and maintain the association between the
subarrays. Go with array_multisort, it would be a one-liner.

What total tosh!  array_multisort() won't handle this one -- usort() is 
correct.  The only function needed is a (one-liner!) custom comparison to 
compare individual [1] elements -- usort() takes care of all the rest:

   function compare_1($a, $b) { return strcmp($a[1], $b[1]); }

   usort($array, 'compare_1');

Or even, for single use, collapse it to:

   usort($array, create_function('$a,$b', 'return strcmp($a[1], $b[1]);');

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211


To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

Hm, yes.. I read the question a bit quick I think, sorry about that. :)

Cheers

--- End Message ---
--- Begin Message ---
Hi everyone, 

In November I sent a mail to this list asking how to get the mail From header 
right, I solved that but I still have a problem. The solution was using 
the -f option like this:

$frommail = "[EMAIL PROTECTED]";
mail("$to", "$subject", "$message", "$headers","-f$frommail");

The from address is correct now but the displayed name is still www-data (or 
apache, depends on the server configuration). The header looks like this:

From: [EMAIL PROTECTED] (www-data)

Is there anyway to change this?
Thanks, regards Chantal

--- End Message ---
--- Begin Message ---
Chantal Rosmuller wrote:
Hi everyone, In November I sent a mail to this list asking how to get the mail From header right, I solved that but I still have a problem. The solution was using the -f option like this:

$frommail = "[EMAIL PROTECTED]";
mail("$to", "$subject", "$message", "$headers","-f$frommail");

The from address is correct now but the displayed name is still www-data (or apache, depends on the server configuration). The header looks like this:

From: [EMAIL PROTECTED] (www-data)

Is there anyway to change this?
Thanks, regards Chantal

$frommail = "[EMAIL PROTECTED]";
mail("$to", "$subject", "$message", "$headers","-f$frommail");

is an extremely ugly way to pass variables IMO.
$frommail = "[EMAIL PROTECTED]";
mail($to, $subject, $message, $headers, "-f".$frommail);

is a lot nicer IMO. Anyway, to get back to you. The "name" you want would be supplied by having the header
From: [EMAIL PROTECTED]
in the following format:
From: MyName Goes Here <[EMAIL PROTECTED]>

--- End Message ---
--- Begin Message ---
Chantal Rosmuller wrote:
Hi everyone, In November I sent a mail to this list asking how to get the mail From header right, I solved that but I still have a problem. The solution was using the -f option like this:

$frommail = "[EMAIL PROTECTED]";
mail("$to", "$subject", "$message", "$headers","-f$frommail");

The from address is correct now but the displayed name is still www-data (or apache, depends on the server configuration). The header looks like this:

From: [EMAIL PROTECTED] (www-data)

Is there anyway to change this?
Thanks, regards Chantal

$frommail = "[EMAIL PROTECTED]";
mail("$to", "$subject", "$message", "$headers","-f$frommail");

is an extremely ugly way to pass variables IMO.
$frommail = "[EMAIL PROTECTED]";
mail($to, $subject, $message, $headers, "-f".$frommail);

is a lot nicer IMO. Anyway, to get back to you. The "name" you want would be supplied by having the header
From: [EMAIL PROTECTED]
in the following format:
From: MyName Goes Here <[EMAIL PROTECTED]>

--- End Message ---
--- Begin Message ---
On Thursday 25 January 2007 08:14, David Robley wrote:
> tedd wrote:
> > At 9:07 PM -0500 1/24/07, Robert Cummings wrote:
>
> <Code structure>
>
> > Ahem to that!
> >
> > You're on a roll brother -- keep going.
> >
> > Can I get another Ahem?!
> >
> > tedd
>
> I'll see your 'Ahem' and raise you an 'Amen' :-)

'n a God Bless.


>
>
>
> Cheers
> --
> David Robley
>
> Vultures only fly with carrion luggage.
> Today is Setting Orange, the 25th day of Chaos in the YOLD 3173.

-- 
---
Børge
Kennel Arivene 
http://www.arivene.net
---

--- End Message ---
--- Begin Message ---
On Thu, January 25, 2007 3:06 am, Sancar Saran wrote:
> On Wednesday 24 January 2007 15:41, Roman Neuhauser wrote:
>> # [EMAIL PROTECTED] / 2007-01-24 13:57:03 +0200:
>> > and also in these days I'm looking for 19 inch (or more) wide LCD
>> > sceerns to able to fit longer lines in my screen...
>>
>> Number of reading errors people make grows with line length,
>> this has been known for as long as I remember.  You're increasing
>> the
>> probability of bugs in the code, and get tired sooner because
>> following
>> long lines requires more energy.
>
> Yes and no, because these days I'm obsessed very very large arrays
> like
> $arr['this']['is']['what']['i']['m']['looking']['for'];
>
> And If I start to do
>
> if( ($arr['this']['is']['what']['i']['m']['looking']['for'] > 5) &&
> ($arr['this']['is']['what']['i']['m']['looking']['for'] < 10))
>
> blah blah
>
> then problem begins :)

Get back to us after you get over your array obsession...
:-)

I rarely find myself using more than 2-D, or occasionally, 3-D array
lookups within any given section of code.

I *might* have deeper arrays, but I'm either going to recurse through
them, or break it down by what's actually in all those layers, and do
something different as I descend.

I would suggest that if one has data nested that deeply, perhaps the
stat structure itself is a poor choice. :-)

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

--- End Message ---
--- Begin Message ---
On Wed, January 24, 2007 8:07 pm, Robert Cummings wrote:
> On Wed, 2007-01-24 at 18:23 -0600, Richard Lynch wrote:
>> On Wed, January 24, 2007 7:41 am, Roman Neuhauser wrote:
>> > # [EMAIL PROTECTED] / 2007-01-24 13:57:03 +0200:
>> >> and also in these days I'm looking for 19 inch (or more) wide LCD
>> >> sceerns to able to fit longer lines in my screen...
>> >
>> > Number of reading errors people make grows with line length,
>> > this has been known for as long as I remember.  You're increasing
>> the
>> > probability of bugs in the code, and get tired sooner because
>> > following
>> > long lines requires more energy.
>>
>> I believe those results are specific to what is being read.
>>
>> Surely it's easier to read:
>>
>> SELECT blah, blah, blah, blah, blah, blah, blah, blah, blah
>>
>> if it's all on one line, no matter how many fields there are, while
>> trying to read the code as a whole.
>>
>> Sure, it can be "hard" to find/read the individual field names, on
>> the
>> rare occasion that you need to do that...
>
> Dear Mr Lynch, normally I highly respect your commentary on the list,
> but today I think you've been-a-smoking the crackpipe a tad too much.
>
> There is no way in hell one long line of SQL is easier to read than
> formatted SQL that clearly delineates the clause structure.
>
> SELECT A.field1 AS afield1, A.field2 AS afield2, B.field1 AS bfield1,
> B.field2 AS bfield2, C.field1 AS cfield1, C.field2 AS cfield2,
> D.field1
> AS dfield1, D.field2 AS dfield2 FROM tableA as A LEFT JOIN tableB AS B
> ON B.fee = A.foo LEFT JOIN tableC AS C ON C.fii = B.fee LEFT JOIN
> tableD
> AS D ON D.fuu = C.fii WHERE A.foo = 'someValue' ORDER BY afield1 ASC,
> cfield2 ASC
>
> The above line "should" be on one line, but my email client might
> autowrap it. Either way, the following is formatted and is much
> clearer.
>
> SELECT
>     A.field1 AS afield1,
>     A.field2 AS afield2,
>     B.field1 AS bfield1,
>     B.field2 AS bfield2,
>     C.field1 AS cfield1,
>     C.field2 AS cfield2,
>     D.field1 AS dfield1,
>     D.field2 AS dfield2
> FROM
>     tableA as A
>         LEFT JOIN tableB AS B ON
>             B.fee = A.foo
>         LEFT JOIN tableC AS C ON
>             C.fii = B.fee
>         LEFT JOIN tableD AS D ON
>             D.fuu = C.fii
> WHERE
>     A.foo = 'someValue'
> ORDER BY
>     afield1 ASC,
>     cfield2 ASC
>
>
> While the above is contrived, most of us know such examples happen
> quite
> often in the wild. Not only is it easier to read, but the task of
> adding
> or removing selected fields is trivial.

I meant ONLY the SELECT part on a single line.

Only a moron would cram the FROM and all that into the same line.
:-)

$query = "SELECT blah1, blah2, blah3, ... blah147 ";
$query .= " FROM table1 ";
$query .= " LEFT OUTER JOIN table2 ";
$query .= "    ON blah7 = blah42 ";
$query .= " WHERE blah16 ";
$query .= "   AND blah42 ";
$query .= " ORDER BY blah9, blah8 desc, blah6 ";

is what I go for.

The SELECT line is the only one that ever gets all that long, really...

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

--- End Message ---
--- Begin Message ---
[EMAIL PROTECTED] / 2007-01-24 23:41:19 -0700:
Just wondering what smart people do for parsing data sent by the Javascript XMLHTTP object--e.g., http.send("post",url,true)...

In a normal form submit, the $_POST global nicely allocates form elements as array elements automatically. But with the AJAX way, the data get stuffed inside $HTTP_RAW_POST_DATA as a string, thereby making extraction more tedious.

Try setting this header before sending your Ajax request:

http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

Then $_POST should have an array, as expected. But $HTTP_RAW_POST_DATA will not be available.

--

_____________________
Myron Turner
http://www.room535.org
http://www.bstatzero.org
http://www.mturner.org/XML_PullParser/

--- End Message ---
--- Begin Message ---

On 25-Jan-07, at 7:49 AM, Myron Turner wrote:

[EMAIL PROTECTED] / 2007-01-24 23:41:19 -0700:
Just wondering what smart people do for parsing data sent by the Javascript XMLHTTP object--e.g., http.send("post",url,true)...

In a normal form submit, the $_POST global nicely allocates form elements as array elements automatically. But with the AJAX way, the data get stuffed inside $HTTP_RAW_POST_DATA as a string, thereby making extraction more tedious.

Try setting this header before sending your Ajax request:

http_request.setRequestHeader("Content-type", "application/x-www- form-urlencoded");

Then $_POST should have an array, as expected. But $HTTP_RAW_POST_DATA will not be available.

Yes, that is the trick, thank you. I didn't realize sending the correct header would then make $_POST do it's magic with "&var=arg into an array", but now it works as desired.

Thanks everyone for the assistance.

...Rene

--- End Message ---
--- Begin Message ---
On Thu, January 25, 2007 12:41 am, M5 wrote:
> Just wondering what smart people do for parsing data sent by the
> Javascript XMLHTTP object--e.g., http.send("post",url,true)...
>
> In a normal form submit, the $_POST global nicely allocates form
> elements as array elements automatically. But with the AJAX way, the
> data get stuffed inside $HTTP_RAW_POST_DATA as a string, thereby
> making extraction more tedious.

Call me crazy, but if AJAX is sending POST data correctly, your PHP
code shouldn't have to do anything special...

POST data is POST data.

The $HTTP_RAW_POST_DATA should be there as well, if you turned that
on, but that doesn't make $_POST go away.

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

--- End Message ---
--- Begin Message ---
On 25-Jan-07, at 4:46 PM, Richard Lynch wrote:

On Thu, January 25, 2007 12:41 am, M5 wrote:
Just wondering what smart people do for parsing data sent by the
Javascript XMLHTTP object--e.g., http.send("post",url,true)...

In a normal form submit, the $_POST global nicely allocates form
elements as array elements automatically. But with the AJAX way, the
data get stuffed inside $HTTP_RAW_POST_DATA as a string, thereby
making extraction more tedious.

Call me crazy, but if AJAX is sending POST data correctly, your PHP
code shouldn't have to do anything special...

You're right in that "*IF* AJAX is sending POST data correctly" everything is okay--that is, will $_POST contain the posted data as array elements.

POST data is POST data.

The $HTTP_RAW_POST_DATA should be there as well, if you turned that
on, but that doesn't make $_POST go away.

Actually, that's not true. If the POST data is not set with the correct headers...

http.setRequestHeader('Content-Type', 'application/x-www-form- urlencoded');
http.setRequestHeader("Content-length", payload.length);
http.setRequestHeader("Connection", "close");

...then $_POST will be empty and the data that is sent can only be accessed from $HTTP_RAW_POST_DATA (which incidentally is off by default).

That was my problem--I wasn't sending those http headers. An earlier poster pointed it out to me, and that solved the problem.

...Rene

--- End Message ---
--- Begin Message ---
Hi Jim, 

Thanks for all the help, but where is the link.  

> Here is a link to a page that has this on it, but with the added "'"
> 
> Plus a link to the source code for it.
> 
> Jim
> 
> --
> PHP General Mailing List (http://www.php.net/) To 
> unsubscribe, visit: http://www.php.net/unsub.php
> 
> 
> 

--- End Message ---
--- Begin Message ---
Beauford wrote:
Hi Jim, Thanks for all the help, but where is the link.
Here is a link to a page that has this on it, but with the added "'"

Plus a link to the source code for it.

Jim

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




sorry

that is what I get for sending things that early in the morning.  :(

http://www.cmsws.com/examples/php/preg_match/example01.php


--
Enjoy,

Jim Lucas

Different eyes see different things. Different hearts beat on different strings. But there are times for you and me when all such things agree.

- Rush

--- End Message ---
--- Begin Message ---
On Thu, January 25, 2007 9:53 am, Jim Lucas wrote:
> http://www.cmsws.com/examples/php/preg_match/example01.php

The \t inside of '' has no special meaning.
So you don't have a TAB character in there.

You need "" to get \t to mean TAB

Once you do that, you should then escape the $ with \$ instead of just
$ in order to be blatantly clear, even though $% is not going to parse
as a variable anyway.

Also, you really ought to use http://php.net/htmlentities on any data
going to the browser, as what we see and what you expect won't match
up otherwise.
echo "String is: '", htmlentities($str), "'<br />\n";

Finally, to be completely pedantic, echoing out raw $_GET data is a
big XSS hole waiting to be exploited.  Start reading here:
http://phpsec.org/

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

--- End Message ---
--- Begin Message ---
Marcelo Ferrufino Murillo wrote:
> Hi guys, I need to send a email to mobiles I don´t know if I have to use
> the
> function "mail( )" or if I have to use other one. Thanks your help
> 

Hi Marcelo,

if the mobile phones you are trying to email have Internet connectivity
the PHP mail() will enable to send you normal email.
If however, you with to text those mobile phones, you'll have to look at
 offers providing email to SMS gateways.

Good luck

--- End Message ---
--- Begin Message ---
Is there a php function I can call to pass in a number and get the values
returned?

For example, pass in 7 and get 1,2,4 ?

Thanks!

--- End Message ---
--- Begin Message ---
If there isn't a function to do exactly what you want, you could use dec2bin() 
to at least get the binary and work from there:

http://us3.php.net/manual/en/function.decbin.php

-TG

= = = Original message = = =

Is there a php function I can call to pass in a number and get the values
returned?

For example, pass in 7 and get 1,2,4 ?

Thanks!


___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

--- End Message ---
--- Begin Message ---
function bits($num) {
       $bit_array = str_split(strrev(decbin(intval($num))));
       $val_array = array();
       foreach ($bit_array as $pow => $bit) {
               if ($val = $bit * pow(2,$pow))
                       $val_array[] = $val;
       }
       return($val_array);
}

(I wanted to see if I could write it in few LOC.) I wonder if there's a faster way...

jon

blackwater dev wrote:
Is there a php function I can call to pass in a number and get the values
returned?

For example, pass in 7 and get 1,2,4 ?

Thanks!


--- End Message ---
--- Begin Message ---
# [EMAIL PROTECTED] / 2007-01-25 14:34:51 -0500:
> function bits($num) {
>        $bit_array = str_split(strrev(decbin(intval($num))));
>        $val_array = array();
>        foreach ($bit_array as $pow => $bit) {
>                if ($val = $bit * pow(2,$pow))
>                        $val_array[] = $val;
>        }
>        return($val_array);
> }
> 
> (I wanted to see if I could write it in few LOC.) I wonder if there's a 
> faster way...

I didn't time either version, and I'm no mathematician either, so this
is prolly a stupid solution.

<?php

function bitarray($ored)
{
    $rv = array();
    for ($v = 1; $v <= $ored; $v *= 2) {
        if ($ored & $v) {
            array_push($rv, $v);
        }
    }
    return $rv;
}

class SingleBitTest extends Tence_TestCase
{
    private function doTest($int)
    {
        return $this->assertEquals(
            array($int),
            bitarray($int)
        );
    }
    function testE_ERROR()
    {
        return $this->doTest(E_ERROR);
    }
    function testE_WARNING()
    {
        return $this->doTest(E_WARNING);
    }
    function testE_NOTICE()
    {
        return $this->doTest(E_NOTICE);
    }
    function testE_USER_ERROR()
    {
        return $this->doTest(E_USER_ERROR);
    }
    function testE_USER_WARNING()
    {
        return $this->doTest(E_USER_WARNING);
    }
    function testE_USER_NOTICE()
    {
        return $this->doTest(E_USER_NOTICE);
    }
}

class BitArrayTest extends Tence_TestCase
{
    private function doTest(array $expected, $int)
    {
        return $this->assertEquals(
            $expected,
            bitarray($int)
        );
    }
    function test7()
    {
        return $this->doTest(array(1, 2, 4), 7);
    }
    function test8()
    {
        return $this->doTest(array(8), 8);
    }
    function testERROR_WARNING_NOTICE_STRICT()
    {
        return $this->doTest(
            array(E_ERROR, E_WARNING, E_NOTICE, E_STRICT),
            E_ERROR|E_WARNING|E_NOTICE|E_STRICT
        );
    }
}

class bttests extends Tence_TestSuite
{
    function __construct()
    {
        $this
            ->add(new SingleBitTest)
            ->add(new BitArrayTest)
        ;
    }
}

?>

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE.             http://bash.org/?255991

--- End Message ---
--- Begin Message ---
Jon Anderson wrote:
function bits($num) {
       $bit_array = str_split(strrev(decbin(intval($num))));
       $val_array = array();
       foreach ($bit_array as $pow => $bit) {
               if ($val = $bit * pow(2,$pow))
                       $val_array[] = $val;
       }
       return($val_array);
}

(I wanted to see if I could write it in few LOC.) I wonder if there's a faster way...

jon

blackwater dev wrote:
Is there a php function I can call to pass in a number and get the values
returned?

For example, pass in 7 and get 1,2,4 ?

Thanks!


and for those of us running PHP<5.x here is a working example

function bits($num) {
        $bit_array = split('.',strrev(decbin(intval($num))));
        $val_array = array();
        foreach ($bit_array as $pow => $bit) {
                if ($val = $bit * pow(2,$pow))
                        $val_array[] = $val;
        }
        return $val_array;
}


--
Enjoy,

Jim Lucas

Different eyes see different things. Different hearts beat on different strings. But there are times for you and me when all such things agree.

- Rush

--- End Message ---
--- Begin Message ---
At 1/25/2007 11:16 AM, blackwater dev wrote:
Is there a php function I can call to pass in a number and get the values
returned?

For example, pass in 7 and get 1,2,4 ?


Here's a slightly more off-the-wall contribution:

====================================
function bin2array($iDecimal)
{
$aResult = array_reverse(explode("\r\n", chunk_split (decbin($iDecimal), 1)));

        array_walk($aResult, 'doPower');

        return $aResult;
}

function doPower(&$iValue, $iIndex)
{
        $iValue = $iValue * pow(2, $iIndex);
}
====================================

Here's the break-down of that eye-crossing first statement:

array_reverse(explode("\r\n", trim(chunk_split (decbin($iDecimal), 1))));

using $iDecimal = 6:

$a = decbin($iDecimal) --> '110'

$b = chunk_split($a, 1) --> '1\r\n1\r\n0'

$c = explode("\r\n", $b);       // array(1,1,0)

$d = array_reverse($c); // array(0,1,1)

(If you're using PHP5 you can use split() instead of chunk_split() and explode().)


The doPower function performs this transform on each member of the array:

$iValue = $iValue * pow(2, $iIndex);

Ix      Val     Math
0       0       0 * 2^0 = 0
1       1       1 * 2^1 = 2
2       1       1 * 2^2 = 4

Regards,

Paul
__________________________

Juniper Webcraft Ltd.
http://juniperwebcraft.com
--- End Message ---
--- Begin Message ---
On Thu, January 25, 2007 1:34 pm, Jon Anderson wrote:
> function bits($num) {
>         $bit_array = str_split(strrev(decbin(intval($num))));
>         $val_array = array();
>         foreach ($bit_array as $pow => $bit) {
>                 if ($val = $bit * pow(2,$pow))
>                         $val_array[] = $val;
>         }
>         return($val_array);
> }
>
> (I wanted to see if I could write it in few LOC.) I wonder if there's
> a
> faster way...

//these might be marginally faster...
function bits($num){
  $bits = array();
  $bin = decbin(intval($num));
  $v = 1;
  for ($b = strlen($bin) - 1; $b >= 0; $b--){
    if ($bin[$b] === '1') $bits[] = $v;
    $v = $v * 2;
  }
  return $bits;
}


function bits($num){
  $num = (int) $num;
  $v = 1;
  $bits = array();
  while ($v <= $num){
    if ($v & $num) $bits[] = $v;
    $v = $v * 2;
  }
  return $bits;
}

I suspect there is a much faster way, somewhere, somehow...

>
> jon
>
> blackwater dev wrote:
>> Is there a php function I can call to pass in a number and get the
>> values
>> returned?
>>
>> For example, pass in 7 and get 1,2,4 ?
>>
>> Thanks!
>>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

--- End Message ---
--- Begin Message ---
Hi,

Sorry if the top is not closely PHP related.  But I need to accomplish it
using PHP.

I can query the attribute 'memberOf' of a user from the active directory
server with no problem.  The challenge I'm facing now is how to obtain all
the groups a user is member of.  In many cases, a user can be in many
groups which could be nested.  Say, user is a member of group B which is a
member of group A.  So user should be member of group A implicitly.  But
in active directory, user's account only has

memberOf:  CN=Group_B,OU=security groups,OU=Users,OU=Coll,DC=some,DC=edu

I can then check if Group_B's LDAP entry has any 'memberOf' attribute, so
on and so on.  If user's LDAP entry has multiple 'memberOf' attributes, I
have to check each one to see if each group has any parent groups. 
Anybody ever had to deal with such a kind of issue and would like to shed
some light (better with some code samples) how it should be done
effectively?  Any ideas would be greatly appreciated.

Thanks,

Bing

--- End Message ---
--- Begin Message ---
On Thu, January 25, 2007 3:07 pm, Bing Du wrote:
> Sorry if the top is not closely PHP related.  But I need to accomplish
> it
> using PHP.
>
> I can query the attribute 'memberOf' of a user from the active
> directory
> server with no problem.  The challenge I'm facing now is how to obtain
> all
> the groups a user is member of.  In many cases, a user can be in many
> groups which could be nested.  Say, user is a member of group B which
> is a
> member of group A.  So user should be member of group A implicitly.
> But
> in active directory, user's account only has
>
> memberOf:  CN=Group_B,OU=security
> groups,OU=Users,OU=Coll,DC=some,DC=edu
>
> I can then check if Group_B's LDAP entry has any 'memberOf' attribute,
> so
> on and so on.  If user's LDAP entry has multiple 'memberOf'
> attributes, I
> have to check each one to see if each group has any parent groups.
> Anybody ever had to deal with such a kind of issue and would like to
> shed
> some light (better with some code samples) how it should be done
> effectively?  Any ideas would be greatly appreciated.

I don't know hardly anything about LDAP, and even less about Active
Directory, but if you can't find a built-in function to do this and
have to write your own, it should end up looking something like:

function groups($user, $groups = null){
  //very first time, initialize $groups to empty array:
  if (is_null($groups)) $groups = array();

  //Find all the groups that his user/group is a memberOf:
  $member_of = //do your LDAP here to find the memberOf:
  //ex: "CN=Group_B,OU=security groups,OU=Users,OU=Coll,DC=some,DC=edu"

  //Look at each group in turn
  $member_of = explode(',', $member_of);
  foreach($member_of as $group){
    //Skip any groups we have already seen:
    if (isset($groups[$group])) continue;

    //Add it to the list of groups:
    $groups[$group] = $group;

    //check for super-groups of this group:
    $groups = array_merge($groups, groups($group, $groups));
  }
}

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

--- End Message ---
--- Begin Message ---
On Thu, January 25, 2007 12:06 am, [EMAIL PROTECTED] wrote:
> Richard Lynch wrote:
>>I dunno what you did wrong with fsockopen...
>
> First of all, thanks for taking the time to respond.
>
> I had tried fsockopen, but here's the problem.  The
> following calls work as expected, returning a valid file
> pointer for valid urls and FALSE for invalid urls:
>
> $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
> $fp = fsockopen("www.youtube.com", 80, $errno, $errstr, 30);
> $fp = fsockopen("www.this_url_is_not_valid.com", 80, $errno, $errstr,
> 30);
>
> The call below does not work and always returns FALSE.
> If I enter the url in a web browser, it works fine, but
> fsockopen does not like it.
>
> $fp = fsockopen("www.youtube.com/v/JqO8ZevPJNk", 80, $errno, $errstr,
> 30);
>
> I think it has something to do with the way YouTube
> works.  Any clues?

What is in $errno and $errstr for YouTube?

If you surf to that URL with LiveHTTPHeaders, what headers are flying
by?  If it's a bunch of re-directs, it's possible the fsockopen is
getting closed immediately after the headers, I guess, so maybe the
socket closes???  That don't sound right.

Still, find out what a browser does, and then mimic that well enough
that YouTube lets you through.

You may end up needing to use http://php.net/curl

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

--- End Message ---
--- Begin Message ---
Re-read the docs more carefully.

The second arg is optional, and it returns the PRIOR state of the
interlaced-ness (or progressive-ness for a JPEG).

Standard computer-science function trick to return prior state when
altering state, and to simply return state if the second arg is not
passed in.

So if you do not pass in a second arg, you should be getting the state
of the JPEG.

Try it.

On Wed, January 24, 2007 7:20 pm, Gerry Danen wrote:
> Richard,
>
> imageinterlace() turns the interlace bit on or off. It only returns 1
> if you set it to 1 as the second parameter...
>
> Thanks
>
> Gerry
>
> On 1/24/07, Richard Lynch <[EMAIL PROTECTED]> wrote:
>> On Wed, January 24, 2007 12:08 am, Gerry Danen wrote:
>> >> One other possibility is to see what happens if you do
>> >> imagefromjpeg()
>> >> on a progressive JPEG -- There amy be functions in GD that will
>> tell
>> >> you if the JPEG is progressive, once you have sucked it into
>> PHP...
>> >
>> > Any idea which ones to look at?
>>
>> No, but a quick search on php.net for "JPEG progress" with "online
>> documentation" from the popup yeilded:
>>
>> http://us3.php.net/manual/en/function.imageinterlace.php
>> "If the interlace bit is set and the image is used as a JPEG image,
>> the image is created as a progressive JPEG.
>>
>> This function returns whether the interlace bit is set for the
>> image. "
>>
>> Presumably, then, this would work:
>>
>> <?php
>>   $filename = '/full/path/to/filename.jpg';
>>   $jpeg = imagecreatefromjpeg($filename);
>>   $progressive = imageinterlace($jpeg);
>>   if ($progressive) echo "$filename is progressive.\n";
>>   else echo "$filename is NOT progressive.\n";
>> ?>
>>
>


-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

--- End Message ---
--- Begin Message ---
I *have* tried, Richard. It is not returning the state of the file.
Files that Irfanview recognizes are prograssive, your example code
does not.

Looking at C code in gd-2.0.33/gd_jpeg.c downloaded from
http://www.boutell.com/gd/ there is a comment:

 /* REMOVED by TBB 2/12/01. This field of the structure is
    documented as private, and sure enough it's gone in the
    latest libjpeg, replaced by something else. Unfortunately
    there is still no right way to find out if the file was
    progressive or not; just declare your intent before you
    write one by calling gdImageInterlace(im, 1) yourself.
    After all, we're not really supposed to rework JPEGs and
    write them out again anyway. Lossy compression, remember? */

The docs at http://www.boutell.com/gd/manual2.0.33.html#gdImageInterlace say

gdImageInterlace is used to determine whether an image should be
stored in a linear fashion, in which lines will appear on the display
from first to last, or in an interlaced fashion, in which the image
will "fade in" over several passes. By default, images are not
interlaced. (When writing JPEG images, interlacing implies generating
progressive JPEG files, which are represented as a series of scans of
increasing quality. Noninterlaced gd images result in regular
[sequential] JPEG data streams.)

A nonzero value for the interlace argument turns on interlace; a zero
value turns it off. Note that interlace has no effect on other
functions, and has no meaning unless you save the image in PNG or JPEG
format; the gd and xbm formats do not support interlace.

When a PNG is loaded with gdImageCreateFromPng or a JPEG is loaded
with gdImageCreateFromJpeg, interlace will be set according to the
setting in the PNG or JPEG file.
--------------

To me that means the code authors cannot determine what the state of
the file is, and are not returning a state they cannot determine.

Gerry




On 1/25/07, Richard Lynch <[EMAIL PROTECTED]> wrote:
Re-read the docs more carefully.

The second arg is optional, and it returns the PRIOR state of the
interlaced-ness (or progressive-ness for a JPEG).

Standard computer-science function trick to return prior state when
altering state, and to simply return state if the second arg is not
passed in.

So if you do not pass in a second arg, you should be getting the state
of the JPEG.

Try it.

On Wed, January 24, 2007 7:20 pm, Gerry Danen wrote:
> Richard,
>
> imageinterlace() turns the interlace bit on or off. It only returns 1
> if you set it to 1 as the second parameter...


--- End Message ---

Reply via email to