Re: [PHP] ltrim behavior.

2009-03-12 Thread tedd

At 3:38 PM -0400 3/11/09, Robert Cummings wrote:


Just because I'm a nice guy... :)


Yeah, me too -- here are three routines I use for cutting the right, 
left and middle portions of strings. These were keyword routines I 
used in FutureBasic -- they seemed to make sense to me so I carried 
them into php.


?php

// == returns the right-most number of characters from a string
// $string = 123456789
// right($string, 3) returns 123

function right($string, $length)
{
$str = substr($string, -$length, $length);
return $str;
}

// == returns the left-most number of characters from a string
// $string = 123456789
// left($string, 3) returns 789

function left($string, $length)
{
$str = substr($string, 0, $length);
return $str;
}

// == returns the middle number of characters from a string 
starting from the left

// $string = 123456789
// mid($string, 3, 4) returns 4567

function mid($string, $left_start, $length)
{
$str = substr($string, $left_start, $length);
return $str;
}
?

Cheers,

tedd
--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

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



Re: [PHP] ltrim behavior.

2009-03-12 Thread tedd

At 5:03 PM -0400 3/11/09, Peter van der Does wrote:


Thanks and I apologize for the stupid question.


The only stupid question is the one that's not asked.

The ltrim() caught me the first time I used it too. I was thinking it 
was trim this string off the left when it was trim these 
characters off the left.


Cheers,

tedd
--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

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



Re: [PHP] ltrim behavior.

2009-03-12 Thread Steve Holmes
On Thu, Mar 12, 2009 at 9:47 AM, tedd tedd.sperl...@gmail.com wrote:

 At 3:38 PM -0400 3/11/09, Robert Cummings wrote:


 Just because I'm a nice guy... :)


 Yeah, me too -- here are three routines I use for cutting the right, left
 and middle portions of strings. These were keyword routines I used in
 FutureBasic -- they seemed to make sense to me so I carried them into php.

 ?php

 // == returns the right-most number of characters from a string
 // $string = 123456789
 // right($string, 3) returns 123

 function right($string, $length)
{
$str = substr($string, -$length, $length);
return $str;
}

 // == returns the left-most number of characters from a string
 // $string = 123456789
 // left($string, 3) returns 789

 function left($string, $length)
{
$str = substr($string, 0, $length);
return $str;
}

 // == returns the middle number of characters from a string starting
 from the left
 // $string = 123456789
 // mid($string, 3, 4) returns 4567

 function mid($string, $left_start, $length)
{
$str = substr($string, $left_start, $length);
return $str;
}
 ?

 Cheers,

 tedd
 --
 ---
 http://sperling.com  http://ancientstones.com  http://earthstones.com


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


Tedd,
Just because I'm a nit-picker, your comments are wrong. Exchange the right
and left comments and it's right.
Steve.


Re: [PHP] ltrim behavior.

2009-03-12 Thread tedd

At 10:18 AM -0400 3/12/09, Steve Holmes wrote:
On Thu, Mar 12, 2009 at 9:47 AM, tedd 
mailto:tedd.sperl...@gmail.comtedd.sperl...@gmail.com wrote:



Tedd,
Just because I'm a nit-picker, your comments are wrong.
Exchange the right and left comments and it's right.
Steve.


Steve:

No, my routines work for me -- I'm dyslexic.  :-)

Seriously, I use these routines all the time and never realized that 
they were reversed.


Thanks (I think),

tedd


--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

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



Re: [PHP] ltrim behavior.

2009-03-12 Thread Robert Cummings
On Thu, 2009-03-12 at 12:46 -0400, tedd wrote:
 At 10:18 AM -0400 3/12/09, Steve Holmes wrote:
 On Thu, Mar 12, 2009 at 9:47 AM, tedd 
 mailto:tedd.sperl...@gmail.comtedd.sperl...@gmail.com wrote:
 
 
 Tedd,
 Just because I'm a nit-picker, your comments are wrong.
 Exchange the right and left comments and it's right.
 Steve.
 
 Steve:
 
 No, my routines work for me -- I'm dyslexic.  :-)

*lol* Job security through dyslexic obscurity!!

;)

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


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



[PHP] ltrim behavior.

2009-03-11 Thread Peter van der Does
This might be old for some of you but I never encountered it until
today and I would like to know why this is happening.

Here's the situation:
php  $a='data[options][name]';
php  echo ltrim($a,'data[');
options][name]

Just as I expected.

Next one:
php  $a='options[options][name]';
php  echo ltrim($a,'options[');
][name]

UH, what?
Not exactly what I expected.

This works:
php  $a='options[options][name]';
php  echo ltrim(ltrim($a,'options'),'[');
options][name]

Can somebody explain the second behavior? Is this a known bug in PHP,
I'm running PHP 5.2.6 on Ubuntu.

-- 
Peter van der Does

GPG key: E77E8E98

WordPress Plugin Developer
http://blog.avirtualhome.com

GetDeb Package Builder/GetDeb Site Coder
http://www.getdeb.net - Software you want for Ubuntu

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



Re: [PHP] ltrim behavior.

2009-03-11 Thread Paul M Foster
On Wed, Mar 11, 2009 at 03:07:18PM -0400, Peter van der Does wrote:

 This might be old for some of you but I never encountered it until
 today and I would like to know why this is happening.
 
 Here's the situation:
 php  $a='data[options][name]';
 php  echo ltrim($a,'data[');
 options][name]
 
 Just as I expected.
 
 Next one:
 php  $a='options[options][name]';
 php  echo ltrim($a,'options[');
 ][name]
 
 UH, what?
 Not exactly what I expected.
 
 This works:
 php  $a='options[options][name]';
 php  echo ltrim(ltrim($a,'options'),'[');
 options][name]
 
 Can somebody explain the second behavior? Is this a known bug in PHP,
 I'm running PHP 5.2.6 on Ubuntu.

Take a look at the documentation for ltrim():

http://us2.php.net/manual/en/function.ltrim.php

The string you're including as the second parameter to ltrim() is the
*characters* you want to trim on, not the *character pattern*. If you
think about it that way, it will make sense. Also one of the examples on
the referenced documentation page does something similar to what you've
cited. Work through that example, and you'll see.

Paul

-- 
Paul M. Foster

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



Re: [PHP] ltrim behavior.

2009-03-11 Thread Robert Cummings
On Wed, 2009-03-11 at 15:07 -0400, Peter van der Does wrote:
 php  $a='data[options][name]';
 php  echo ltrim($a,'data[');
 options][name]
 
 Just as I expected.
 
 Next one:
 php  $a='options[options][name]';
 php  echo ltrim($a,'options[');
 ][name]
 
 UH, what?
 Not exactly what I expected.
 
 This works:
 php  $a='options[options][name]';
 php  echo ltrim(ltrim($a,'options'),'[');
 options][name]

It doesn't trim strings, it trims characters. The second argument is a
list of characters. I'm surprised this has worked for you up till now. I
think most people would use the following to do what you want:

?php

echo preg_replace( '#^options\[#', '', $a )

?

Or they would do test and strip:

?php

if( substr( $a, 0, 8 ) === 'options[' )
{
echo substr( $a, 8 );
}

?

Or maybe:

?php

if( preg_match( '#^options\[#', $a ) )
{
echo substr( $a, 8 );
}

?

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


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



Re: [PHP] ltrim behavior.

2009-03-11 Thread Robert Cummings
On Wed, 2009-03-11 at 15:34 -0400, Robert Cummings wrote:
 On Wed, 2009-03-11 at 15:07 -0400, Peter van der Does wrote:
  php  $a='data[options][name]';
  php  echo ltrim($a,'data[');
  options][name]
  
  Just as I expected.
  
  Next one:
  php  $a='options[options][name]';
  php  echo ltrim($a,'options[');
  ][name]
  
  UH, what?
  Not exactly what I expected.
  
  This works:
  php  $a='options[options][name]';
  php  echo ltrim(ltrim($a,'options'),'[');
  options][name]
 
 It doesn't trim strings, it trims characters. The second argument is a
 list of characters. I'm surprised this has worked for you up till now. I
 think most people would use the following to do what you want:
 
 ?php
 
 echo preg_replace( '#^options\[#', '', $a )
 
 ?
 
 Or they would do test and strip:
 
 ?php
 
 if( substr( $a, 0, 8 ) === 'options[' )
 {
 echo substr( $a, 8 );
 }
 
 ?
 
 Or maybe:
 
 ?php
 
 if( preg_match( '#^options\[#', $a ) )
 {
 echo substr( $a, 8 );
 }
 
 ?

Just because I'm a nice guy... :) here's the generic strip:

?php

echo preg_replace( '#^[[:alpha:]]+[[:alnum:]]*\[#', '', $a );

?

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


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



Re: [PHP] ltrim behavior.

2009-03-11 Thread Peter van der Does
On Wed, 11 Mar 2009 15:28:04 -0400
Paul M Foster pa...@quillandmouse.com wrote:

 On Wed, Mar 11, 2009 at 03:07:18PM -0400, Peter van der Does wrote:
 
  This might be old for some of you but I never encountered it until
  today and I would like to know why this is happening.
  
  Here's the situation:
  php  $a='data[options][name]';
  php  echo ltrim($a,'data[');
  options][name]
  
  Just as I expected.
  
  Next one:
  php  $a='options[options][name]';
  php  echo ltrim($a,'options[');
  ][name]
  
  UH, what?
  Not exactly what I expected.
  
  This works:
  php  $a='options[options][name]';
  php  echo ltrim(ltrim($a,'options'),'[');
  options][name]
  
  Can somebody explain the second behavior? Is this a known bug in
  PHP, I'm running PHP 5.2.6 on Ubuntu.
 
 Take a look at the documentation for ltrim():
 
 http://us2.php.net/manual/en/function.ltrim.php
 
 The string you're including as the second parameter to ltrim() is the
 *characters* you want to trim on, not the *character pattern*. If you
 think about it that way, it will make sense. Also one of the examples
 on the referenced documentation page does something similar to what
 you've cited. Work through that example, and you'll see.
 
 Paul
 
OK, the light bulb went on now. I did see it was characters, and not a
string, but for some reason it didn't get in my brain what the
consequence was.
Thanks and I apologize for the stupid question.


-- 
Peter van der Does

GPG key: E77E8E98
IRC: Ganseki on irc.freenode.net
Blog: http://blog.avirtualhome.com
Forums: http://forums.avirtualhome.com
Jabber ID: pvanderd...@gmail.com

GetDeb Package Builder
http://www.getdeb.net - Software you want for Ubuntu

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