php-general Digest 1 Aug 2008 18:31:15 -0000 Issue 5601

Topics (messages 277575 through 277593):

HTTP PUT for file uploads
        277575 by: mike
        277577 by: Micah Gersten
        277578 by: mike

PHP 5.3.0alpha1
        277576 by: Lukas Kahwe Smith

Re: Back to Basics - Why Use Single Quotes?
        277579 by: Peter Ford

Writing my own web based user forum
        277580 by: Jason Pruim
        277581 by: Bernhard Kohl
        277582 by: Jason Pruim
        277583 by: Yeti
        277584 by: Jason Pruim
        277591 by: Eric Butera

Internationalisation and MB strings
        277585 by: Yeti
        277586 by: Andrew Ballard
        277587 by: Yeti
        277588 by: Bernhard Kohl
        277589 by: Boyd, Todd M.
        277590 by: Robert Cummings
        277592 by: Andrew Ballard
        277593 by: Robert Cummings

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 ---
It appears that PHP can support the PUT method using php://stdin and
appropriately configuring the webserver to accept it.

My company needs a file upload solution that will support large file
uploads (2GB limit is optional - if we have to tell them less than 2GB
that's fine) that will keep re-trying the upload until it is done. We
have slow geo users and then just flat out large files to deal with
even from fast connections.

There's a variety of Java-based PUT uploaders.

So far, we haven't found any Flash ones (we'd love to NOT use Java) -
but there is a way to do it apparently, we just can't find anyone
who's done it yet.

I'm assuming that we should keep the connection open as long as there
is some activity and maybe timeout after a minute or two... the
client-side applet should have the logic to continue retrying and
since it is PUT, the PHP script will accept the data and use fseek()
on the file to resume at the offset supplied (the client will have to
give us that info)

See the examples here:
http://www.radinks.com/upload/examples/ - look at the "Handlers that
support resume" section.

Anyone have any thoughts? I think I need to tweak PHP settings
too possibly as well, for max execution time and such. But also any
uploader ideas would be great.

The reason for using this is FTP/SFTP require logins or some sort of
"pick up" process or two step process to first upload the file then
have the user associate it (or a cronjob somehow associate and move
it) to it's final destination. HTTP isn't the best for file uploads
but it appears PUT does support resuming, and we just want the
cleanest possible frontend to it. Java stuff is slow, Flash would be
better, but it appears Flash only supports basic POST/GET and you have
to use a third party library (and possibly the latest Flex?) to be
able to support other HTTP methods. If anyone has any products or
knows of any projects, open source solutions would be best but money
is not an object basically so we'd be open to commercial ones as well.
We want the least amount of work for the end-user, so no thick clients
and hopefully the most compact [cross-platform] browser applet as
well. (I am assuming Flash does finally work on Linux)

--- End Message ---
--- Begin Message ---
Is this a repetitive thing your clients will do many times?  I recently
created a backup solution using ssh keys and the pecl ssh extension to
automate backups.  Then a cronjob sorts the files on the server.  It's a
lot more secure than allowing PUTs.

Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com



mike wrote:
> It appears that PHP can support the PUT method using php://stdin and
> appropriately configuring the webserver to accept it.
>
> My company needs a file upload solution that will support large file
> uploads (2GB limit is optional - if we have to tell them less than 2GB
> that's fine) that will keep re-trying the upload until it is done. We
> have slow geo users and then just flat out large files to deal with
> even from fast connections.
>
> There's a variety of Java-based PUT uploaders.
>
> So far, we haven't found any Flash ones (we'd love to NOT use Java) -
> but there is a way to do it apparently, we just can't find anyone
> who's done it yet.
>
> I'm assuming that we should keep the connection open as long as there
> is some activity and maybe timeout after a minute or two... the
> client-side applet should have the logic to continue retrying and
> since it is PUT, the PHP script will accept the data and use fseek()
> on the file to resume at the offset supplied (the client will have to
> give us that info)
>
> See the examples here:
> http://www.radinks.com/upload/examples/ - look at the "Handlers that
> support resume" section.
>
> Anyone have any thoughts? I think I need to tweak PHP settings
> too possibly as well, for max execution time and such. But also any
> uploader ideas would be great.
>
> The reason for using this is FTP/SFTP require logins or some sort of
> "pick up" process or two step process to first upload the file then
> have the user associate it (or a cronjob somehow associate and move
> it) to it's final destination. HTTP isn't the best for file uploads
> but it appears PUT does support resuming, and we just want the
> cleanest possible frontend to it. Java stuff is slow, Flash would be
> better, but it appears Flash only supports basic POST/GET and you have
> to use a third party library (and possibly the latest Flex?) to be
> able to support other HTTP methods. If anyone has any products or
> knows of any projects, open source solutions would be best but money
> is not an object basically so we'd be open to commercial ones as well.
> We want the least amount of work for the end-user, so no thick clients
> and hopefully the most compact [cross-platform] browser applet as
> well. (I am assuming Flash does finally work on Linux)
>
>   

--- End Message ---
--- Begin Message ---
On 8/1/08, Micah Gersten <[EMAIL PROTECTED]> wrote:

> Is this a repetitive thing your clients will do many times?  I recently
> created a backup solution using ssh keys and the pecl ssh extension to
> automate backups.  Then a cronjob sorts the files on the server.  It's a
> lot more secure than allowing PUTs.

Yes, we have end users (non-employees) and other employees from all
geographies (slow connections in China, India), USA, europe, etc, etc.
uploading files of any size and any shape. Video files, audio files,
executables, PDFs, anything.

This is a general purpose file upload solution for our content
management system used worldwide. We can't really create a list of
users and such, as it's open to anyone to contribute. We still
moderate/filter/etc. as needed. We're not worried about security, each
file will have its own unique location so they wouldn't clobber each
other, and they'll be isolated and not executed on the server, so
security to me isn't a concern. The webserver is already open to the
world, SSH/SFTP and FTP are not; so that would technically open that
up to the world, and actually open up more attack vectors :)

--- End Message ---
--- Begin Message ---
Hello!

Johannes has packed PHP 5.3.0alpha1 yesterday evening, which you can find here:
http://downloads.php.net/johannes/

Please test it carefully, and report any bugs in the bug system, but only if you have a short reproducable test case. The final release is expected sometime between mid September and mid October. You can read more information about this release here:
http://www.php.net/archive/2008.php#id2008-08-01-1

regards,
Johannes and Lukas



--- End Message ---
--- Begin Message ---
Eric Butera wrote:
On Wed, Jul 30, 2008 at 6:51 PM, Stephen <[EMAIL PROTECTED]> wrote:
I have traditionally used double quotes going back to my PASCAL days.

I see many PHP examples using single quotes, and I began to adopt that
convention.

Even updating existing code.

And I broke some stuff that was doing variable expansion. So I am back to
using double quotes.

But I wonder, is there any reason to use single quotes?

Stephen

Because sometimes it is nice to do this:

$onclick = ' onclick="..."'

Not having to escape my single tick makes it more readable.


Now *that's* a good reason... anything that means fewer \'s or \"s.
I often go for HEREDOC syntax if the backslashes start taking over - readability is much more important to me than nanoseconds of performance.

There's also the case where you might want literal $s in your string:
e.g.
$foo = 'You need to pay $dollars...';

compared with

$foo = "You need to pay \$dollars...";

Again, it depends on you view of backslashes.


--
Peter Ford                              phone: 01580 893333
Developer                               fax:   01580 893399
Justcroft International Ltd., Staplehurst, Kent

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

I am looking at writing my own web based user forum somewhat for my own knowledge, and because I know that I don't know enough to be able to tie in authentication so they only need 1 user account.

Does anyone have any examples short of downloading something like phpbb and tearing it apart?

It will probably be really light weight, basic forum that hopefully will expand into more as time goes on :)

I've tried searching the web but have not been able to turn up any help unless I was to write a bridge for a song...

Any knowledge you can bestow upon me is greatly appreciated!

Thanks!


--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
11287 James St
Holland, MI 49424
www.raoset.com
[EMAIL PROTECTED]





--- End Message ---
--- Begin Message ---
<?php
#User authentication should not be a problem at all.
#If you want it simple just serialize a user class and write it to a
file or into a DB.

class user {
 var $username;
 var $pw;
 function set_user($name, $pw) {
  $this->pw = md5($pw);
  $this->username = $name;
  return true;
 }
}

$new_user = new user();
if ($new_user->set_user('Joe', 'swordfish') {
 $fp =@ fopen('/some_dir/users.txt', 'a');
 @fwrite($fp, serialize($new_user));
 @fclose($fp);
}

# the next time you need it simple read it in again with unserialize()

?>
On Fri, Aug 1, 2008 at 1:49 PM, Jason Pruim <[EMAIL PROTECTED]> wrote:
> Hi Everyone,
>
> I am looking at writing my own web based user forum somewhat for my own
> knowledge, and because I know that I don't know enough to be able to tie in
> authentication so they only need 1 user account.
>
> Does anyone have any examples short of downloading something like phpbb and
> tearing it apart?
>
> It will probably be really light weight, basic forum that hopefully will
> expand into more as time goes on :)
>
> I've tried searching the web but have not been able to turn up any help
> unless I was to write a bridge for a song...
>
> Any knowledge you can bestow upon me is greatly appreciated!
>
> Thanks!
>
>
> --
>
> Jason Pruim
> Raoset Inc.
> Technology Manager
> MQC Specialist
> 11287 James St
> Holland, MI 49424
> www.raoset.com
> [EMAIL PROTECTED]
>
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---
--- Begin Message --- So are you suggesting, create the users in the forum, and then just serialize/unserialize when needed to store the info so when they go to the picture upload side it queries the forum database?


On Aug 1, 2008, at 8:07 AM, Bernhard Kohl wrote:

<?php
#User authentication should not be a problem at all.
#If you want it simple just serialize a user class and write it to a
file or into a DB.

class user {
var $username;
var $pw;
function set_user($name, $pw) {
 $this->pw = md5($pw);
 $this->username = $name;
 return true;
}
}

$new_user = new user();
if ($new_user->set_user('Joe', 'swordfish') {
$fp =@ fopen('/some_dir/users.txt', 'a');
@fwrite($fp, serialize($new_user));
@fclose($fp);
}

# the next time you need it simple read it in again with unserialize()

?>
On Fri, Aug 1, 2008 at 1:49 PM, Jason Pruim <[EMAIL PROTECTED]> wrote:
Hi Everyone,

I am looking at writing my own web based user forum somewhat for my own knowledge, and because I know that I don't know enough to be able to tie in
authentication so they only need 1 user account.

Does anyone have any examples short of downloading something like phpbb and
tearing it apart?

It will probably be really light weight, basic forum that hopefully will
expand into more as time goes on :)

I've tried searching the web but have not been able to turn up any help
unless I was to write a bridge for a song...

Any knowledge you can bestow upon me is greatly appreciated!

Thanks!


--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
11287 James St
Holland, MI 49424
www.raoset.com
[EMAIL PROTECTED]





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



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



--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
11287 James St
Holland, MI 49424
www.raoset.com
[EMAIL PROTECTED]





--- End Message ---
--- Begin Message ---
If I fully understood the question you were asking then no.
Since it seems you are using a database there is no need to serialize.
You can write a simple function that fetches user data from a DB query
and then creates the object.
If you are going to use sessions you will have to do that only when
the user logs in and for verification purposes of whatever kind.
You might think of something similar to:
http://webbytedd.com/b1/simple-session/

In your original post you did not mention a "picture upload site" so I
suggest it is about uploading pictures to the forum/attaching them to
a thread.
I do not really have an idea of how your site will be operating, but I
guess you analyze the GET parameters and include files according to
them.
Some code might be helpful to explain what you want

On Fri, Aug 1, 2008 at 2:52 PM, Jason Pruim <[EMAIL PROTECTED]> wrote:
> So are you suggesting, create the users in the forum, and then just
> serialize/unserialize when needed to store the info so when they go to the
> picture upload side it queries the forum database?
>
>
> On Aug 1, 2008, at 8:07 AM, Bernhard Kohl wrote:
>
>> <?php
>> #User authentication should not be a problem at all.
>> #If you want it simple just serialize a user class and write it to a
>> file or into a DB.
>>
>> class user {
>> var $username;
>> var $pw;
>> function set_user($name, $pw) {
>>  $this->pw = md5($pw);
>>  $this->username = $name;
>>  return true;
>> }
>> }
>>
>> $new_user = new user();
>> if ($new_user->set_user('Joe', 'swordfish') {
>> $fp =@ fopen('/some_dir/users.txt', 'a');
>> @fwrite($fp, serialize($new_user));
>> @fclose($fp);
>> }
>>
>> # the next time you need it simple read it in again with unserialize()
>>
>> ?>
>> On Fri, Aug 1, 2008 at 1:49 PM, Jason Pruim <[EMAIL PROTECTED]> wrote:
>>>
>>> Hi Everyone,
>>>
>>> I am looking at writing my own web based user forum somewhat for my own
>>> knowledge, and because I know that I don't know enough to be able to tie
>>> in
>>> authentication so they only need 1 user account.
>>>
>>> Does anyone have any examples short of downloading something like phpbb
>>> and
>>> tearing it apart?
>>>
>>> It will probably be really light weight, basic forum that hopefully will
>>> expand into more as time goes on :)
>>>
>>> I've tried searching the web but have not been able to turn up any help
>>> unless I was to write a bridge for a song...
>>>
>>> Any knowledge you can bestow upon me is greatly appreciated!
>>>
>>> Thanks!
>>>
>>>
>>> --
>>>
>>> Jason Pruim
>>> Raoset Inc.
>>> Technology Manager
>>> MQC Specialist
>>> 11287 James St
>>> Holland, MI 49424
>>> www.raoset.com
>>> [EMAIL PROTECTED]
>>>
>>>
>>>
>>>
>>>
>>> --
>>> PHP General Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>
>>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
> --
>
> Jason Pruim
> Raoset Inc.
> Technology Manager
> MQC Specialist
> 11287 James St
> Holland, MI 49424
> www.raoset.com
> [EMAIL PROTECTED]
>
>
>
>
>

--- End Message ---
--- Begin Message --- The problem with that is I don't have any code to show since I'm going to be writing it :)

Basically, it's a site where people can upload pictures to compare with other's (Non-porn) so I need to write an authentication system for that, but that is taken care of... Another aspect of the site though is a User Forum where the users can interact with each and talk about what ever they want.

So, I need to either:
#1.     Write my own web forum. OR
#2. Integrate a currently existing one to work with my authentication system.

I'm leaning towards #1 since it sounds easier to me at this point then #2.

Clear as mud now? :)


On Aug 1, 2008, at 9:08 AM, Yeti wrote:

If I fully understood the question you were asking then no.
Since it seems you are using a database there is no need to serialize.
You can write a simple function that fetches user data from a DB query
and then creates the object.
If you are going to use sessions you will have to do that only when
the user logs in and for verification purposes of whatever kind.
You might think of something similar to:
http://webbytedd.com/b1/simple-session/

In your original post you did not mention a "picture upload site" so I
suggest it is about uploading pictures to the forum/attaching them to
a thread.
I do not really have an idea of how your site will be operating, but I
guess you analyze the GET parameters and include files according to
them.
Some code might be helpful to explain what you want

On Fri, Aug 1, 2008 at 2:52 PM, Jason Pruim <[EMAIL PROTECTED]> wrote:
So are you suggesting, create the users in the forum, and then just
serialize/unserialize when needed to store the info so when they go to the
picture upload side it queries the forum database?


On Aug 1, 2008, at 8:07 AM, Bernhard Kohl wrote:

<?php
#User authentication should not be a problem at all.
#If you want it simple just serialize a user class and write it to a
file or into a DB.

class user {
var $username;
var $pw;
function set_user($name, $pw) {
$this->pw = md5($pw);
$this->username = $name;
return true;
}
}

$new_user = new user();
if ($new_user->set_user('Joe', 'swordfish') {
$fp =@ fopen('/some_dir/users.txt', 'a');
@fwrite($fp, serialize($new_user));
@fclose($fp);
}

# the next time you need it simple read it in again with unserialize()

?>
On Fri, Aug 1, 2008 at 1:49 PM, Jason Pruim <[EMAIL PROTECTED]> wrote:

Hi Everyone,

I am looking at writing my own web based user forum somewhat for my own knowledge, and because I know that I don't know enough to be able to tie
in
authentication so they only need 1 user account.

Does anyone have any examples short of downloading something like phpbb
and
tearing it apart?

It will probably be really light weight, basic forum that hopefully will
expand into more as time goes on :)

I've tried searching the web but have not been able to turn up any help
unless I was to write a bridge for a song...

Any knowledge you can bestow upon me is greatly appreciated!

Thanks!


--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
11287 James St
Holland, MI 49424
www.raoset.com
[EMAIL PROTECTED]





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



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



--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
11287 James St
Holland, MI 49424
www.raoset.com
[EMAIL PROTECTED]







--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
11287 James St
Holland, MI 49424
www.raoset.com
[EMAIL PROTECTED]





--- End Message ---
--- Begin Message ---
On Fri, Aug 1, 2008 at 9:27 AM, Jason Pruim <[EMAIL PROTECTED]> wrote:
> The problem with that is I don't have any code to show since I'm going to be
> writing it :)
>
> Basically, it's a site where people can upload pictures to compare with
> other's (Non-porn) so I need to write an authentication system for that, but
> that is taken care of... Another aspect of the site though is a User Forum
> where the users can interact with each and talk about what ever they want.
>
> So, I need to either:
> #1.     Write my own web forum. OR
> #2.     Integrate a currently existing one to work with my authentication
> system.
>
> I'm leaning towards #1 since it sounds easier to me at this point then #2.
>
> Clear as mud now? :)

Start by figuring out your requirements.  Do you want to allow
categories, threads, comments, quoting, administrator, moderator, etc.
 Current bb's need to be everything to everyone.  It will have a lot
of stuff you don't want or won't need.  It is usually smarter trying
to use an existing solution though.  I wouldn't recommend phpbb.  But
what I mean is that they have gone through the process of figuring out
requirements, implementing features, testing, and deploying on lots of
different setups.  Doing something yourself throws away all those
hours of work.  Usually these things have teams of people, not just
one Rambo, not that there's anything wrong with that! :)  Just keep it
in mind.

After that figure out how to implement each feature you want.

There is a big difference between authenticating a user and a user's
authorization.  You can use combinations of Zend_Auth and Zend_Acl to
manage this fairly easily.  Or you can come up with your own scheme.
Each user should get their own row in a users table.  Then each user
needs some sort of user/pass.  You shouldn't store raw passwords,
always hash them using something such as sha.  The roles/permissions
can be impemented with the ACL (access control list) by assigning each
part of your system as an action that can have roles attached to it.
An example would be administrator can view all users or logged in user
can upload photos.

--- End Message ---
--- Begin Message ---
<?php
*# Hello Community
# Internationalisation, a topic discussed more than enough and YES, I am
looking forward to PHP6.
# But in reality I still have to develop for PHP4 and that's where the dog
is burried ^^
# We have a customer here who is running a small site, but still in five
different languages.
# Lately he started complaining about some strange site behaviours:

# He has a discussion board where people can post their ideas, comments etc.
Nothing special
# Every post has a maximum length of 2048 characters, which is checked by
JavaScript at the Browser
# and after submitting the form by PHP.

# Our mistake was to use strlen();*
global $cc_strlen; global $cc_mb;
$cc_strlen = $cc_mb = 0;
if (array_key_exists('text', $_POST)) {
 $cc_strlen = strlen($_POST['text']);
 $cc_mb = mb_strlen($_POST['text'], 'UTF-8'); *// new code*
 if ($cc_strlen > 2048) { /* snip */ } // do something
}

/* snip */ // do something

*#this works fine as long as the user only submits single byte charachters,
but with UTF-8 the whole thing changes ..*
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml";>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head>
<body>
<p>You submitted <?php echo $cc_strlen; ?> characters (STRLEN).</p>
<p>You submitted <?php echo $cc_mb; ?> characters (MB_STRLEN).</p>
<p>Characters Left:<span id="remainder">2048</span></p>
<form action="" method="post" onsubmit="return false;" id="post_form">
<textarea id="post_text" name="text" onkeydown="check_length();"
onchange="check_length();" rows="10" cols="50">œŸŒ‡Ņ</textarea><br />
<input type="submit" value="Submit" id="post_button"
onclick="submit_form();" />
</form>
<script type="text/javascript">
<!--
var the_form = document.getElementById('post_form');
var textarea = document.getElementById('post_text');
var counter = document.getElementById('remainder');
function check_length() {
 var remainder = 2048 - textarea.value.length;
 var length_alert = false;
 if (remainder < 0) {
  remainder = 0;
  for (var count = textarea.value.length; (count >= 2048); (count -= 1)) {
  textarea.value = textarea.value.substr(0, 2047);
  counter.style.color = 'red'
  length_alert = true;
  }
 }
 if (length_alert) alert('You are already using 2048 characters.');
 if (document.all) {
  counter.innerText = remainder;
 } else {
  counter.textContent = remainder;
 }
}
function submit_form() {
 check_length();
 the_form.submit();
 alert ('You submitted ' + textarea.value.length + ' characters');
 return true;
}
-->
</script>
<?php
*# Now as soon as one is starting to submit UTF-8 characters strlen is not
working proberly any more
# So we had to work through thousands of lines of code, replacing strlen()
with mb_strlen();
# We also found mb_strlen to take about 8 times longer than strlen().*

$s_t = microtime();
mb_strlen('œŸŒ‡Ņ', 'UTF-8');
$e_t = microtime();
echo '<p>MB_STRLEN took : '.(($e_t - $s_t)*1000).' milliseconds</p>';
$s_t = microtime();
strlen('œŸŒ‡Ņ');
$e_t = microtime();
echo '<p>STRLEN took : '.(($e_t - $s_t)*1000).' milliseconds</p>';

*# So much for internationalisation.
# Just writing this as a reminder for everyone who is facing similar
situations.*
?>
</body>
</html>

--- End Message ---
--- Begin Message ---
On Fri, Aug 1, 2008 at 9:50 AM, Yeti <[EMAIL PROTECTED]> wrote:
> <?php
> *# Hello Community
> # Internationalisation, a topic discussed more than enough and YES, I am
> looking forward to PHP6.
> # But in reality I still have to develop for PHP4 and that's where the dog
> is burried ^^
> # We have a customer here who is running a small site, but still in five
> different languages.
> # Lately he started complaining about some strange site behaviours:
>
> # He has a discussion board where people can post their ideas, comments etc.
> Nothing special
> # Every post has a maximum length of 2048 characters, which is checked by
> JavaScript at the Browser
> # and after submitting the form by PHP.
>
> # Our mistake was to use strlen();*
> global $cc_strlen; global $cc_mb;
> $cc_strlen = $cc_mb = 0;
> if (array_key_exists('text', $_POST)) {
>  $cc_strlen = strlen($_POST['text']);
>  $cc_mb = mb_strlen($_POST['text'], 'UTF-8'); *// new code*
>  if ($cc_strlen > 2048) { /* snip */ } // do something
> }
>
> /* snip */ // do something
>
> *#this works fine as long as the user only submits single byte charachters,
> but with UTF-8 the whole thing changes ..*
> ?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
> http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
> <html xmlns="http://www.w3.org/1999/xhtml";>
> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
> <title>test</title>
> </head>
> <body>
> <p>You submitted <?php echo $cc_strlen; ?> characters (STRLEN).</p>
> <p>You submitted <?php echo $cc_mb; ?> characters (MB_STRLEN).</p>
> <p>Characters Left:<span id="remainder">2048</span></p>
> <form action="" method="post" onsubmit="return false;" id="post_form">
> <textarea id="post_text" name="text" onkeydown="check_length();"
> onchange="check_length();" rows="10" cols="50">œŸŒ‡Ņ</textarea><br />
> <input type="submit" value="Submit" id="post_button"
> onclick="submit_form();" />
> </form>
> <script type="text/javascript">
> <!--
> var the_form = document.getElementById('post_form');
> var textarea = document.getElementById('post_text');
> var counter = document.getElementById('remainder');
> function check_length() {
>  var remainder = 2048 - textarea.value.length;
>  var length_alert = false;
>  if (remainder < 0) {
>  remainder = 0;
>  for (var count = textarea.value.length; (count >= 2048); (count -= 1)) {
>  textarea.value = textarea.value.substr(0, 2047);
>  counter.style.color = 'red'
>  length_alert = true;
>  }
>  }
>  if (length_alert) alert('You are already using 2048 characters.');
>  if (document.all) {
>  counter.innerText = remainder;
>  } else {
>  counter.textContent = remainder;
>  }
> }
> function submit_form() {
>  check_length();
>  the_form.submit();
>  alert ('You submitted ' + textarea.value.length + ' characters');
>  return true;
> }
> -->
> </script>
> <?php
> *# Now as soon as one is starting to submit UTF-8 characters strlen is not
> working proberly any more
> # So we had to work through thousands of lines of code, replacing strlen()
> with mb_strlen();
> # We also found mb_strlen to take about 8 times longer than strlen().*
>
> $s_t = microtime();
> mb_strlen('œŸŒ‡Ņ', 'UTF-8');
> $e_t = microtime();
> echo '<p>MB_STRLEN took : '.(($e_t - $s_t)*1000).' milliseconds</p>';
> $s_t = microtime();
> strlen('œŸŒ‡Ņ');
> $e_t = microtime();
> echo '<p>STRLEN took : '.(($e_t - $s_t)*1000).' milliseconds</p>';
>
> *# So much for internationalisation.
> # Just writing this as a reminder for everyone who is facing similar
> situations.*
> ?>
> </body>
> </html>
>

You can't determine timing by simply calling each function one time. I
changed your script to the following:

<?php

$iterations = 10000;

$s_t = microtime(true);
for ($i = 0; $i < $iterations; ++$i) {
    mb_strlen('œŸŒ‡Ņ', 'UTF-8');
}
$e_t = microtime(true);
echo '<p>MB_STRLEN took : '.(($e_t - $s_t)*1000/$iterations).'
milliseconds</p>';

$s_t = microtime(true);
for ($i = 0; $i < $iterations; ++$i) {
    strlen('œŸŒ‡Ņ');
}
$e_t = microtime(true);
echo '<p>STRLEN took : '.(($e_t - $s_t)*1000/$iterations).' milliseconds</p>';

?>

I ran this script several times, and the results below are fairly typical:

MB_STRLEN took : 0.054733037948608 milliseconds

STRLEN took : 0.037568092346191 milliseconds


The multi-byte function is slower, but not even by a factor of 2 on my
development machine.

Andrew

--- End Message ---
--- Begin Message ---
Oh right. Doing 1 measurement only is not even worth a theory.

Well, I'm wondering how much PHP can speed that result up, since we are
calling the same function with the same parameter 10000 times. Wouldn't it
be even more realistic if we called it with changing strings?

<?php

$iterations = 10000;
$mb_array = array();
for ($i = 0; $i < $iterations; ++$i) $mb_array[] = str_shuffle('œŸŒ‡Ņ');
$s_t = microtime(true);
foreach ($mb_array as $mb_string) {
 mb_strlen($test_string, 'UTF-8');
}
$e_t = microtime(true);
echo '<p>MB_STRLEN took : '.(($e_t - $s_t)*1000/$iterations).'
milliseconds</p>';

$s_t = microtime(true);
foreach ($mb_array as $mb_string) {
 strlen('œŸŒ‡Ņ');
}
$e_t = microtime(true);
echo '<p>STRLEN took : '.(($e_t - $s_t)*1000/$iterations).'
milliseconds</p>';

?>

MB_STRLEN took : 0.0525826 milliseconds

STRLEN took : 0.0020655 milliseconds

I could not find out how well str_shuffle supports multi byte strings in
PHP4, so I'm wondering if I did this right ..

On Fri, Aug 1, 2008 at 5:06 PM, Andrew Ballard <[EMAIL PROTECTED]> wrote:

> On Fri, Aug 1, 2008 at 9:50 AM, Yeti <[EMAIL PROTECTED]> wrote:
> > <?php
> > *# Hello Community
> > # Internationalisation, a topic discussed more than enough and YES, I am
> > looking forward to PHP6.
> > # But in reality I still have to develop for PHP4 and that's where the
> dog
> > is burried ^^
> > # We have a customer here who is running a small site, but still in five
> > different languages.
> > # Lately he started complaining about some strange site behaviours:
> >
> > # He has a discussion board where people can post their ideas, comments
> etc.
> > Nothing special
> > # Every post has a maximum length of 2048 characters, which is checked by
> > JavaScript at the Browser
> > # and after submitting the form by PHP.
> >
> > # Our mistake was to use strlen();*
> > global $cc_strlen; global $cc_mb;
> > $cc_strlen = $cc_mb = 0;
> > if (array_key_exists('text', $_POST)) {
> >  $cc_strlen = strlen($_POST['text']);
> >  $cc_mb = mb_strlen($_POST['text'], 'UTF-8'); *// new code*
> >  if ($cc_strlen > 2048) { /* snip */ } // do something
> > }
> >
> > /* snip */ // do something
> >
> > *#this works fine as long as the user only submits single byte
> charachters,
> > but with UTF-8 the whole thing changes ..*
> > ?>
> > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
> > http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
> > <html xmlns="http://www.w3.org/1999/xhtml";>
> > <head>
> > <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
> > <title>test</title>
> > </head>
> > <body>
> > <p>You submitted <?php echo $cc_strlen; ?> characters (STRLEN).</p>
> > <p>You submitted <?php echo $cc_mb; ?> characters (MB_STRLEN).</p>
> > <p>Characters Left:<span id="remainder">2048</span></p>
> > <form action="" method="post" onsubmit="return false;" id="post_form">
> > <textarea id="post_text" name="text" onkeydown="check_length();"
> > onchange="check_length();" rows="10" cols="50">œŸŒ‡Ņ</textarea><br />
> > <input type="submit" value="Submit" id="post_button"
> > onclick="submit_form();" />
> > </form>
> > <script type="text/javascript">
> > <!--
> > var the_form = document.getElementById('post_form');
> > var textarea = document.getElementById('post_text');
> > var counter = document.getElementById('remainder');
> > function check_length() {
> >  var remainder = 2048 - textarea.value.length;
> >  var length_alert = false;
> >  if (remainder < 0) {
> >  remainder = 0;
> >  for (var count = textarea.value.length; (count >= 2048); (count -= 1)) {
> >  textarea.value = textarea.value.substr(0, 2047);
> >  counter.style.color = 'red'
> >  length_alert = true;
> >  }
> >  }
> >  if (length_alert) alert('You are already using 2048 characters.');
> >  if (document.all) {
> >  counter.innerText = remainder;
> >  } else {
> >  counter.textContent = remainder;
> >  }
> > }
> > function submit_form() {
> >  check_length();
> >  the_form.submit();
> >  alert ('You submitted ' + textarea.value.length + ' characters');
> >  return true;
> > }
> > -->
> > </script>
> > <?php
> > *# Now as soon as one is starting to submit UTF-8 characters strlen is
> not
> > working proberly any more
> > # So we had to work through thousands of lines of code, replacing
> strlen()
> > with mb_strlen();
> > # We also found mb_strlen to take about 8 times longer than strlen().*
> >
> > $s_t = microtime();
> > mb_strlen('œŸŒ‡Ņ', 'UTF-8');
> > $e_t = microtime();
> > echo '<p>MB_STRLEN took : '.(($e_t - $s_t)*1000).' milliseconds</p>';
> > $s_t = microtime();
> > strlen('œŸŒ‡Ņ');
> > $e_t = microtime();
> > echo '<p>STRLEN took : '.(($e_t - $s_t)*1000).' milliseconds</p>';
> >
> > *# So much for internationalisation.
> > # Just writing this as a reminder for everyone who is facing similar
> > situations.*
> > ?>
> > </body>
> > </html>
> >
>
> You can't determine timing by simply calling each function one time. I
> changed your script to the following:
>
> <?php
>
> $iterations = 10000;
>
> $s_t = microtime(true);
> for ($i = 0; $i < $iterations; ++$i) {
>     mb_strlen('œŸŒ‡Ņ', 'UTF-8');
> }
> $e_t = microtime(true);
> echo '<p>MB_STRLEN took : '.(($e_t - $s_t)*1000/$iterations).'
> milliseconds</p>';
>
> $s_t = microtime(true);
> for ($i = 0; $i < $iterations; ++$i) {
>    strlen('œŸŒ‡Ņ');
> }
> $e_t = microtime(true);
> echo '<p>STRLEN took : '.(($e_t - $s_t)*1000/$iterations).'
> milliseconds</p>';
>
> ?>
>
> I ran this script several times, and the results below are fairly typical:
>
> MB_STRLEN took : 0.054733037948608 milliseconds
>
> STRLEN took : 0.037568092346191 milliseconds
>
>
> The multi-byte function is slower, but not even by a factor of 2 on my
> development machine.
>
> Andrew
>

--- End Message ---
--- Begin Message ---
/* snippetty */

foreach ($mb_array as $mb_string) {

 strlen('œŸŒ‡Ņ');
}

/* snip */

Oh, this is supposed to be a *strlen($mb_string)*; of course

>

--- End Message ---
--- Begin Message ---
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Yeti
> Sent: Friday, August 01, 2008 10:58 AM
> To: Andrew Ballard
> Cc: PHP General list
> Subject: Re: [PHP] Internationalisation and MB strings
> 
> Oh right. Doing 1 measurement only is not even worth a theory.
> 
> Well, I'm wondering how much PHP can speed that result up, since we are
> calling the same function with the same parameter 10000 times. Wouldn't
> it
> be even more realistic if we called it with changing strings?

---8<--- snip

> > I ran this script several times, and the results below are fairly
> typical:
> >
> > MB_STRLEN took : 0.054733037948608 milliseconds
> >
> > STRLEN took : 0.037568092346191 milliseconds
> >
> >
> > The multi-byte function is slower, but not even by a factor of 2 on
> my
> > development machine.

10000 * 0.02 = 200 milliseconds.

I don't think you've got anything to worry about... unless counting to "2" is 
an unacceptable wait for your process. :)


Todd Boyd
Web Programmer




--- End Message ---
--- Begin Message ---
On Fri, 2008-08-01 at 11:12 -0500, Boyd, Todd M. wrote:
> > -----Original Message-----
> > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Yeti
> > Sent: Friday, August 01, 2008 10:58 AM
> > To: Andrew Ballard
> > Cc: PHP General list
> > Subject: Re: [PHP] Internationalisation and MB strings
> > 
> > Oh right. Doing 1 measurement only is not even worth a theory.
> > 
> > Well, I'm wondering how much PHP can speed that result up, since we are
> > calling the same function with the same parameter 10000 times. Wouldn't
> > it
> > be even more realistic if we called it with changing strings?
> 
> ---8<--- snip
> 
> > > I ran this script several times, and the results below are fairly
> > typical:
> > >
> > > MB_STRLEN took : 0.054733037948608 milliseconds
> > >
> > > STRLEN took : 0.037568092346191 milliseconds

How did you measure these? I don't recall a time function that returns
milliseconds... only functions that return seconds (and parts thereof in
microseconds)... such as microtime().

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


--- End Message ---
--- Begin Message ---
On Fri, Aug 1, 2008 at 12:34 PM, Robert Cummings <[EMAIL PROTECTED]> wrote:
> On Fri, 2008-08-01 at 11:12 -0500, Boyd, Todd M. wrote:
>> > -----Original Message-----
>> > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Yeti
>> > Sent: Friday, August 01, 2008 10:58 AM
>> > To: Andrew Ballard
>> > Cc: PHP General list
>> > Subject: Re: [PHP] Internationalisation and MB strings
>> >
>> > Oh right. Doing 1 measurement only is not even worth a theory.
>> >
>> > Well, I'm wondering how much PHP can speed that result up, since we are
>> > calling the same function with the same parameter 10000 times. Wouldn't
>> > it
>> > be even more realistic if we called it with changing strings?
>>
>> ---8<--- snip
>>
>> > > I ran this script several times, and the results below are fairly
>> > typical:
>> > >
>> > > MB_STRLEN took : 0.054733037948608 milliseconds
>> > >
>> > > STRLEN took : 0.037568092346191 milliseconds
>
> How did you measure these? I don't recall a time function that returns
> milliseconds... only functions that return seconds (and parts thereof in
> microseconds)... such as microtime().
>
> Cheers,
> Rob.

If microtime(true) returns fractional seconds, wouldn't multiplying
the result by 1000 (as he did) convert the units from seconds to
milliseconds?

1 second = 1000 milliseconds = 1000000 microseconds, thus 0.000037568
seconds = 0.037568 milliseconds, correct? Am I missing something?

Andrew

--- End Message ---
--- Begin Message ---
On Fri, 2008-08-01 at 13:24 -0400, Andrew Ballard wrote:
> On Fri, Aug 1, 2008 at 12:34 PM, Robert Cummings <[EMAIL PROTECTED]> wrote:
> > On Fri, 2008-08-01 at 11:12 -0500, Boyd, Todd M. wrote:
> >> > -----Original Message-----
> >> > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Yeti
> >> > Sent: Friday, August 01, 2008 10:58 AM
> >> > To: Andrew Ballard
> >> > Cc: PHP General list
> >> > Subject: Re: [PHP] Internationalisation and MB strings
> >> >
> >> > Oh right. Doing 1 measurement only is not even worth a theory.
> >> >
> >> > Well, I'm wondering how much PHP can speed that result up, since we are
> >> > calling the same function with the same parameter 10000 times. Wouldn't
> >> > it
> >> > be even more realistic if we called it with changing strings?
> >>
> >> ---8<--- snip
> >>
> >> > > I ran this script several times, and the results below are fairly
> >> > typical:
> >> > >
> >> > > MB_STRLEN took : 0.054733037948608 milliseconds
> >> > >
> >> > > STRLEN took : 0.037568092346191 milliseconds
> >
> > How did you measure these? I don't recall a time function that returns
> > milliseconds... only functions that return seconds (and parts thereof in
> > microseconds)... such as microtime().
> >
> > Cheers,
> > Rob.
> 
> If microtime(true) returns fractional seconds, wouldn't multiplying
> the result by 1000 (as he did) convert the units from seconds to
> milliseconds?
> 
> 1 second = 1000 milliseconds = 1000000 microseconds, thus 0.000037568
> seconds = 0.037568 milliseconds, correct? Am I missing something?

I didn't see the multiplication by 1000 and the above numbers don't show
any trailing zeros or truncation of precision as I would expect if the
number had been multiplied by 1000. Although, maybe the multiplication
occurred before the division by the number of runs. It just looked
suspect to me.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


--- End Message ---

Reply via email to