php-general Digest 5 Jul 2006 04:50:32 -0000 Issue 4222

Topics (messages 239118 through 239133):

Re: Bitwise operators and check if an bit is NOT within the flag.
        239118 by: Jochem Maas
        239120 by: Mathijs
        239121 by: Jochem Maas
        239122 by: Mathijs
        239123 by: Mathijs
        239124 by: Mathijs
        239125 by: Mathijs

Re: running multiple updates on a single line
        239119 by: M. Sokolewicz

Re: Templates, PHP Frameworks, and DB Abstraction?
        239126 by: Manuel Lemos
        239127 by: Manuel Lemos
        239133 by: Paul Scott

What's the regex for this...?
        239128 by: Brian Dunning
        239129 by: Paul Novitski
        239130 by: Chris

uploading and extracting zip files
        239131 by: Schalk
        239132 by: Chris

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:
        [email protected]


----------------------------------------------------------------------
--- Begin Message ---
Mathijs wrote:
> Hello there.
> 
> I am working with some bitwise Operators for validating some variables.
> Now i need to know if an certain bit is NOT set and an other bit IS set.
> 
> Example.
> 
> <?php
> 
> const VALIDATE_CHECK1 = 1;
> const VALIDATE_CHECK2 = 2;
> const VALIDATE_CHECK3 = 4;
> const VALIDATE_ALL    = 7;
> 
> //--Example 1 - This works nice.
> $flag1 = self::VALIDATE_CHECK1;
> 
> //Do if VALIDATE_CHECK1 is set
> if ($flag1 & self::VALIDATE_CHECK1) {
>     print 'Validate 1';
> }
> //Do if VALIDATE_CHECK2 is set
> if ($flag1 & self::VALIDATE_CHECK2) {
>     print 'Validate 2';
> }
> 
> //--Example 2 - I want to check if VALIDATE_CHECK3 is not set and then
> continue.
> $flag2 = self::VALIDATE_ALL;
> 
> //Do if VALIDATE_CHECK1 is set BUT NOT when VALIDATE_CHECK3 is set.
> if ($flag2 & self::VALIDATE_CHECK1 && $flag2 & ~self::VALIDATE_CHECK3) {

class Test {
const VALIDATE_CHECK1 = 1;
const VALIDATE_CHECK2 = 2;
const VALIDATE_CHECK3 = 4;
const VALIDATE_ALL    = 7;
static function check($flag2)
{
        if (($flag2 & self::VALIDATE_CHECK1) &&
            !($flag2 & self::VALIDATE_CHECK3)) print "Only Validate 1";
}
}

echo "First attempt: ";
Test::check( Test::VALIDATE_ALL );
echo "\nSecond attempt: ";
Test::check( Test::VALIDATE_CHECK1 );
echo "\n";
>     print 'Only Validate 1';
> }
> //etc...
> ?>
> 
> This last example i can't seem to get to work.
> I Want to only do that when for example VALIDATE_CHECK3 is not within
> the $flag2.
> How can i do this?
> 
> Thx in advanced.
> 

--- End Message ---
--- Begin Message ---
Jochem Maas wrote:
Mathijs wrote:
Hello there.

I am working with some bitwise Operators for validating some variables.
Now i need to know if an certain bit is NOT set and an other bit IS set.

Example.

<?php

const VALIDATE_CHECK1 = 1;
const VALIDATE_CHECK2 = 2;
const VALIDATE_CHECK3 = 4;
const VALIDATE_ALL    = 7;

//--Example 1 - This works nice.
$flag1 = self::VALIDATE_CHECK1;

//Do if VALIDATE_CHECK1 is set
if ($flag1 & self::VALIDATE_CHECK1) {
    print 'Validate 1';
}
//Do if VALIDATE_CHECK2 is set
if ($flag1 & self::VALIDATE_CHECK2) {
    print 'Validate 2';
}

//--Example 2 - I want to check if VALIDATE_CHECK3 is not set and then
continue.
$flag2 = self::VALIDATE_ALL;

//Do if VALIDATE_CHECK1 is set BUT NOT when VALIDATE_CHECK3 is set.
if ($flag2 & self::VALIDATE_CHECK1 && $flag2 & ~self::VALIDATE_CHECK3) {

class Test {
const VALIDATE_CHECK1 = 1;
const VALIDATE_CHECK2 = 2;
const VALIDATE_CHECK3 = 4;
const VALIDATE_ALL    = 7;
static function check($flag2)
{
        if (($flag2 & self::VALIDATE_CHECK1) &&
            !($flag2 & self::VALIDATE_CHECK3)) print "Only Validate 1";
}
}

echo "First attempt: ";
Test::check( Test::VALIDATE_ALL );
echo "\nSecond attempt: ";
Test::check( Test::VALIDATE_CHECK1 );
echo "\n";
    print 'Only Validate 1';
}
//etc...
?>

This last example i can't seem to get to work.
I Want to only do that when for example VALIDATE_CHECK3 is not within
the $flag2.
How can i do this?

Thx in advanced.



Thank you very much.
This seems to work :).

--- End Message ---
--- Begin Message ---
Mathijs wrote:
> Jochem Maas wrote:
>> Mathijs wrote:


...

>>>
> 
> 
> Thank you very much.
> This seems to work :).

cool. heres's a couple of funcs that might help you to understand bitwise
operations better:

<?php

/* whether there is only 1 single bit set or not */
function single_bit_set(/*int*/ $i)
{
    // beware: if $i is zero !($i & ~get_ls1bit($i)) returns true;
    return $i ? !($i & ~get_ls1bit($i)): false;
}

/* return the value of the least significant bit */
function get_ls1bit(/*int*/ $i)
{
    for ($j = 1; $i && !($i & $j); $j <<= 1);
    return $i ? $j : 0;
}

/* return the value of the most significant bit */
function get_ms1bit(/*int*/ $i)
{
    $x = 0;
    for ($j = $i; $i && !($j == 1); $j >>= 1) { $x++; }
    return $i ? $j <<= $x: 0;
}

/* doesn't break when exponents are near the wordsize
 * of the machine as the native xor does, here is some example code to
 * illustrate:

php -r '
    // include function definition here
    $x = 3851235679;
    $y = 43814;
    echo "\nThis is the value we want";
    echo "\n3851262585";

    echo "\nThe result of a native xor operation on integer values is treated 
as a signed integer";
    echo "\n".($x ^ $y);

    echo "\nWe therefore perform the MSB separately";
    echo "\n".get_xor32($x, $y)."\n";
'

 */
function get_xor32(/*int*/ $a, /*int*/ $b)
{
       $a1 = $a & 0x7FFF0000;
       $a2 = $a & 0x0000FFFF;
       $a3 = $a & 0x80000000;
       $b1 = $b & 0x7FFF0000;
       $b2 = $b & 0x0000FFFF;
       $b3 = $b & 0x80000000;

       $c = ($a3 != $b3) ? 0x80000000 : 0;

       return (($a1 ^ $b1) |($a2 ^ $b2)) + $c;
}

function get_bit_str($var, $safety = 0)
{
    $rtn = '';

    $var = intval($var);
    if ($var < 0) { $var = 0 - $var; }

    while ($var != 0 /*&& $safety < 31*/) {
        $rtn .= ($var & 1);
        $var >>= 1;
        $safety++;
    }

    return $rtn;
}

> 

--- End Message ---
--- Begin Message ---
Jochem Maas wrote:
Mathijs wrote:
Jochem Maas wrote:
Mathijs wrote:


...


Thank you very much.
This seems to work :).

cool. heres's a couple of funcs that might help you to understand bitwise
operations better:

<?php

/* whether there is only 1 single bit set or not */
function single_bit_set(/*int*/ $i)
{
    // beware: if $i is zero !($i & ~get_ls1bit($i)) returns true;
    return $i ? !($i & ~get_ls1bit($i)): false;
}

/* return the value of the least significant bit */
function get_ls1bit(/*int*/ $i)
{
    for ($j = 1; $i && !($i & $j); $j <<= 1);
    return $i ? $j : 0;
}

/* return the value of the most significant bit */
function get_ms1bit(/*int*/ $i)
{
    $x = 0;
    for ($j = $i; $i && !($j == 1); $j >>= 1) { $x++; }
    return $i ? $j <<= $x: 0;
}

/* doesn't break when exponents are near the wordsize
 * of the machine as the native xor does, here is some example code to
 * illustrate:

php -r '
    // include function definition here
    $x = 3851235679;
    $y = 43814;
    echo "\nThis is the value we want";
    echo "\n3851262585";

    echo "\nThe result of a native xor operation on integer values is treated as a 
signed integer";
    echo "\n".($x ^ $y);

    echo "\nWe therefore perform the MSB separately";
    echo "\n".get_xor32($x, $y)."\n";
'

 */
function get_xor32(/*int*/ $a, /*int*/ $b)
{
       $a1 = $a & 0x7FFF0000;
       $a2 = $a & 0x0000FFFF;
       $a3 = $a & 0x80000000;
       $b1 = $b & 0x7FFF0000;
       $b2 = $b & 0x0000FFFF;
       $b3 = $b & 0x80000000;

       $c = ($a3 != $b3) ? 0x80000000 : 0;

       return (($a1 ^ $b1) |($a2 ^ $b2)) + $c;
}

function get_bit_str($var, $safety = 0)
{
    $rtn = '';

    $var = intval($var);
    if ($var < 0) { $var = 0 - $var; }

    while ($var != 0 /*&& $safety < 31*/) {
        $rtn .= ($var & 1);
        $var >>= 1;
        $safety++;
    }

    return $rtn;
}

Thx alot :).

It will sure help me understanding it better :).

--- End Message ---
--- Begin Message ---
Jochem Maas wrote:
Mathijs wrote:
Jochem Maas wrote:
Mathijs wrote:


...


Thank you very much.
This seems to work :).

cool. heres's a couple of funcs that might help you to understand bitwise
operations better:

<?php

/* whether there is only 1 single bit set or not */
function single_bit_set(/*int*/ $i)
{
    // beware: if $i is zero !($i & ~get_ls1bit($i)) returns true;
    return $i ? !($i & ~get_ls1bit($i)): false;
}

/* return the value of the least significant bit */
function get_ls1bit(/*int*/ $i)
{
    for ($j = 1; $i && !($i & $j); $j <<= 1);
    return $i ? $j : 0;
}

/* return the value of the most significant bit */
function get_ms1bit(/*int*/ $i)
{
    $x = 0;
    for ($j = $i; $i && !($j == 1); $j >>= 1) { $x++; }
    return $i ? $j <<= $x: 0;
}

/* doesn't break when exponents are near the wordsize
 * of the machine as the native xor does, here is some example code to
 * illustrate:

php -r '
    // include function definition here
    $x = 3851235679;
    $y = 43814;
    echo "\nThis is the value we want";
    echo "\n3851262585";

    echo "\nThe result of a native xor operation on integer values is treated as a 
signed integer";
    echo "\n".($x ^ $y);

    echo "\nWe therefore perform the MSB separately";
    echo "\n".get_xor32($x, $y)."\n";
'

 */
function get_xor32(/*int*/ $a, /*int*/ $b)
{
       $a1 = $a & 0x7FFF0000;
       $a2 = $a & 0x0000FFFF;
       $a3 = $a & 0x80000000;
       $b1 = $b & 0x7FFF0000;
       $b2 = $b & 0x0000FFFF;
       $b3 = $b & 0x80000000;

       $c = ($a3 != $b3) ? 0x80000000 : 0;

       return (($a1 ^ $b1) |($a2 ^ $b2)) + $c;
}

function get_bit_str($var, $safety = 0)
{
    $rtn = '';

    $var = intval($var);
    if ($var < 0) { $var = 0 - $var; }

    while ($var != 0 /*&& $safety < 31*/) {
        $rtn .= ($var & 1);
        $var >>= 1;
        $safety++;
    }

    return $rtn;
}

Thx alot :).

It will sure help me understanding it better :).

--- End Message ---
--- Begin Message ---
Jochem Maas wrote:
Mathijs wrote:
Jochem Maas wrote:
Mathijs wrote:


...


Thank you very much.
This seems to work :).

cool. heres's a couple of funcs that might help you to understand bitwise
operations better:

<?php

/* whether there is only 1 single bit set or not */
function single_bit_set(/*int*/ $i)
{
    // beware: if $i is zero !($i & ~get_ls1bit($i)) returns true;
    return $i ? !($i & ~get_ls1bit($i)): false;
}

/* return the value of the least significant bit */
function get_ls1bit(/*int*/ $i)
{
    for ($j = 1; $i && !($i & $j); $j <<= 1);
    return $i ? $j : 0;
}

/* return the value of the most significant bit */
function get_ms1bit(/*int*/ $i)
{
    $x = 0;
    for ($j = $i; $i && !($j == 1); $j >>= 1) { $x++; }
    return $i ? $j <<= $x: 0;
}

/* doesn't break when exponents are near the wordsize
 * of the machine as the native xor does, here is some example code to
 * illustrate:

php -r '
    // include function definition here
    $x = 3851235679;
    $y = 43814;
    echo "\nThis is the value we want";
    echo "\n3851262585";

    echo "\nThe result of a native xor operation on integer values is treated as a 
signed integer";
    echo "\n".($x ^ $y);

    echo "\nWe therefore perform the MSB separately";
    echo "\n".get_xor32($x, $y)."\n";
'

 */
function get_xor32(/*int*/ $a, /*int*/ $b)
{
       $a1 = $a & 0x7FFF0000;
       $a2 = $a & 0x0000FFFF;
       $a3 = $a & 0x80000000;
       $b1 = $b & 0x7FFF0000;
       $b2 = $b & 0x0000FFFF;
       $b3 = $b & 0x80000000;

       $c = ($a3 != $b3) ? 0x80000000 : 0;

       return (($a1 ^ $b1) |($a2 ^ $b2)) + $c;
}

function get_bit_str($var, $safety = 0)
{
    $rtn = '';

    $var = intval($var);
    if ($var < 0) { $var = 0 - $var; }

    while ($var != 0 /*&& $safety < 31*/) {
        $rtn .= ($var & 1);
        $var >>= 1;
        $safety++;
    }

    return $rtn;
}

Thx alot :).

It will sure help me understanding it better :).

--- End Message ---
--- Begin Message ---
Jochem Maas wrote:
Mathijs wrote:
Jochem Maas wrote:
Mathijs wrote:


...


Thank you very much.
This seems to work :).

cool. heres's a couple of funcs that might help you to understand bitwise
operations better:

<?php

/* whether there is only 1 single bit set or not */
function single_bit_set(/*int*/ $i)
{
    // beware: if $i is zero !($i & ~get_ls1bit($i)) returns true;
    return $i ? !($i & ~get_ls1bit($i)): false;
}

/* return the value of the least significant bit */
function get_ls1bit(/*int*/ $i)
{
    for ($j = 1; $i && !($i & $j); $j <<= 1);
    return $i ? $j : 0;
}

/* return the value of the most significant bit */
function get_ms1bit(/*int*/ $i)
{
    $x = 0;
    for ($j = $i; $i && !($j == 1); $j >>= 1) { $x++; }
    return $i ? $j <<= $x: 0;
}

/* doesn't break when exponents are near the wordsize
 * of the machine as the native xor does, here is some example code to
 * illustrate:

php -r '
    // include function definition here
    $x = 3851235679;
    $y = 43814;
    echo "\nThis is the value we want";
    echo "\n3851262585";

    echo "\nThe result of a native xor operation on integer values is treated as a 
signed integer";
    echo "\n".($x ^ $y);

    echo "\nWe therefore perform the MSB separately";
    echo "\n".get_xor32($x, $y)."\n";
'

 */
function get_xor32(/*int*/ $a, /*int*/ $b)
{
       $a1 = $a & 0x7FFF0000;
       $a2 = $a & 0x0000FFFF;
       $a3 = $a & 0x80000000;
       $b1 = $b & 0x7FFF0000;
       $b2 = $b & 0x0000FFFF;
       $b3 = $b & 0x80000000;

       $c = ($a3 != $b3) ? 0x80000000 : 0;

       return (($a1 ^ $b1) |($a2 ^ $b2)) + $c;
}

function get_bit_str($var, $safety = 0)
{
    $rtn = '';

    $var = intval($var);
    if ($var < 0) { $var = 0 - $var; }

    while ($var != 0 /*&& $safety < 31*/) {
        $rtn .= ($var & 1);
        $var >>= 1;
        $safety++;
    }

    return $rtn;
}

Thx alot :).

It will sure help me understanding it better :).

--- End Message ---
--- Begin Message ---
Ryan A wrote:


--- Chris <[EMAIL PROTECTED]> wrote:


Ryan A wrote:

Hi,
in phpmyadmin, in the SQL part where you can write

a

query if I have a double update such as this:


it works without a problem as I am ending each sql
statement with a simicolon, but in my scripts when

I

try to run multiple updates in a single line
eg:
$xyz="update xyz set id=1 where id=0;update xyz

set

id=3 where id=2;";


it does not work... any idea why?



-->Chris
try a newline between the queries then phpmyadmin
will see them as separate queries and run them properly.


@Chris, Tried that... no joy.


-->João

Could any of these lines cousing an error?


@João, nope, I broke them down to different
statements , put them into an array and ran them in a
for() loop and it works perfectly... problem is,
sometimes I have to run upto 15 updates at a time,
would be great if I can run that in one statement.

--> Robert

RTFM :) -- mysql_query()


Ok, read it, again.... what am I missing?
I took out the semicolon from the end... then I get an
error.
I did find one user post saying that this runs just
one query at a time...but if thats the case how is
PHPMyAdmin running multiple statements that are
seperated by a semicolon?

Thanks!
Ryan
because it manually splits them on the semicolon and runs each one trough mysql_query separately.
--- End Message ---
--- Begin Message ---
Hello,

on 07/03/2006 03:38 AM Lester Caine said the following:
>>> I'd like to get some feedback on what the list thinks is a good template
>>> engine other than smarty.
>>>
>>> I'd also like to do some quick prototyping using a PHP framework does
>>> anyone
>>> have any recommendations for one that is easy to pick up and run with?
>>>
>>> Finally, does anyone have any suggestions for a good PHP database
>>> abstraction library?
>>
>> You may want to take a look at this post for a few recommendations:
>>
>> http://www.phpclasses.org/blog/post/52-Recommended-PHP-frameworks.html
> 
> That is a nice sales pitch Manuel but takes  bit of digesting.

If you were looking for specific recommendations, they are at the end of
the post.

Anyway, the post was more to say that there is no consense regarding
what is the recommended framework for PHP. Most people end up picking
components for different purposes from different sources because
different developers have developed the best packages for their own
purposes.

-- 

Regards,
Manuel Lemos

Metastorage - Data object relational mapping layer generator
http://www.metastorage.net/

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

--- End Message ---
--- Begin Message ---
Hello,

on 07/03/2006 05:22 PM Jay Paulson said the following:
>>>> I'd like to get some feedback on what the list thinks is a good template
>>>> engine other than smarty.
>>>>
>>>> I'd also like to do some quick prototyping using a PHP framework does 
>>>> anyone
>>>> have any recommendations for one that is easy to pick up and run with?
>>>>
>>>> Finally, does anyone have any suggestions for a good PHP database
>>>> abstraction library?
>>> You may want to take a look at this post for a few recommendations:
>>>
>>> http://www.phpclasses.org/blog/post/52-Recommended-PHP-frameworks.html
>> That is a nice sales pitch Manuel but takes  bit of digesting.
>>
>> Jay - This is a 'how long is a piece of string' type question. So there
>> are as many answers as there are developers here ;)
>>
>> I've only ever used Smarty - and not seen anything yet to replace it.
>>
>> PDO is being pushed as a DB Abstraction library, but it only 'abstracts'
>> the calls to PHP, it does nothing to abstract the SQL if you want a
>> truly generic solution, if you need one, but if you don't then why
>> bother with abstraction ;)
> 
> Thinking about this paragraph above makes me wonder if with that way of
> looking at it would one need a template engine?  For example, why not just
> separate the business logic and the HTML as much as possible and then only
> imbed PHP in HTML to display the variables (<?= $var ?>)?? Then at the end
> of your PHP code that does all the logic just have include() calls to the
> .php files you want to parse and pass all the variables you got with your
> business logic above the include calls? Hm.  Just a thought.

You think like that because you work alone or with other PHP developers.

The truth is that good PHP developers are often not good Web (page)
designers, and good Web designers are not good developers.

Therefore it makes sense to split the Web page design work from PHP
programming work.

Smarty templates are easier to learn by Web designers than PHP. They do
not need to know PHP.

Furthermore, it is not a good idea to leave Web designers that does not
know PHP very well to write PHP code as they may write insecure code and
you will have to double the work by auditing the  PHP based templates.


-- 

Regards,
Manuel Lemos

Metastorage - Data object relational mapping layer generator
http://www.metastorage.net/

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

--- End Message ---
--- Begin Message ---
On Tue, 2006-07-04 at 18:54 -0300, Manuel Lemos wrote:

> The truth is that good PHP developers are often not good Web (page)
> designers, and good Web designers are not good developers.

Yes, I agree with this completely! I am a relatively decent developer,
but ask me to design an interface and you make a blabbering idiot of
me... 

In our framework, Chisimba (Framework in a bunch of African languages),
we completely separate the logic components from the templates and view
(MVC). The nice thing about working in this way is that all of our code
is modular as well as OO, so that if I write an object it can be
re-used, as well as form only a small part of a module. This means that
it is possible to get multiple developers and designers all working
simultaneously on a single module to get it done really quickly.

Also, to take your original topic a little further, we not only abstract
database calls, but also everything else. For example, in an e-learning
context we call our Content system a course, in a CMS implementation a
Content Area, and in a groupware implementation a Work Area. This in
multiple languages makes for a highly configurable set up! Keep all of
that in mind when designing your app, as it caters for the widest
possible audience, as well as the project contributing significantly to
other projects. (Our project has contributed around 15 or so classes to
phpclasses.org, bugfixes and feature requests to a couple of pear
projects, amongst others).

--Paul

All Email originating from UWC is covered by disclaimer  
http://www.uwc.ac.za/portal/uwc2006/content/mail_disclaimer/index.htm 

--- End Message ---
--- Begin Message --- I have the source of a web page stored in $here and I want to set $this to whatever appears between '<span class="myclass">' and '</ span>'. Example:

$here contains (amid tons of other stuff): <span class="myclass">This is a statement.</span>

I want to set $this to "This is a statement." Seems simple but I'm banging my head against a wall and can't figure out how to do it... can anyone help out a dude on the 4th of July?? :) :)

- Brian

--- End Message ---
--- Begin Message ---
At 04:50 PM 7/4/2006, Brian Dunning wrote:
I have the source of a web page stored in $here and I want to set
$this to whatever appears between '<span class="myclass">' and '</ span>'. Example:

$here contains (amid tons of other stuff):  <span
class="myclass">This is a statement.</span>


If you can depend on the markup you provided, you can use:

        $this = preg_replace("/<[^>]+>/", "", $here);

to replace any <tag> with nothing. The regular expression /<[^>]+>/ parses to <{one or more characters that aren't >}>

Paul
--- End Message ---
--- Begin Message ---
Brian Dunning wrote:
I have the source of a web page stored in $here and I want to set $this to whatever appears between '<span class="myclass">' and '</span>'. Example:

$here contains (amid tons of other stuff): <span class="myclass">This is a statement.</span>

I want to set $this to "This is a statement."

What code do you have so far?

if it's strictly between those tags, this should work:

preg_match_all('%<span class="myclass">(.*?)</span>%isU', $text, $matches);

$matches[1] will be an array with all matches.

--
Postgresql & php tutorials
http://www.designmagick.com/

--- End Message ---
--- Begin Message ---
Greetings All,

Can someone please point me to a tutorial or open source 'library' that will explain how one can upload a .zip file and then extract it's contents and store this on the server and/or database using PHP. Basically the same way as one can upload and install components etc. with Joomla. Thanks!

--
Kind Regards
Schalk Neethling
Web Developer.Designer.Programmer.President
Volume4.Business.Solution.Developers

--- End Message ---
--- Begin Message ---
Schalk wrote:
Greetings All,

Can someone please point me to a tutorial or open source 'library' that will explain how one can upload a .zip file and then extract it's contents and store this on the server and/or database using PHP.

This will unzip a file:

http://pear.php.net/package/Archive_Zip

As for uploading, you'll need something else for that (I don't think there's something to do it *all* for you but I could be wrong).

--
Postgresql & php tutorials
http://www.designmagick.com/

--- End Message ---

Reply via email to