p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: using chunk_split() and strrev()


Message #1 by Jacob Halpert <jhalpert@m...> on Mon, 23 Sep 2002 14:39:52 -0400
Hello all,

I am trying to handle some user input. I have a form text field in which the
user inputs up to a five digit number. If the user puts a comma in before
the last three digits (e.g. 1,000 or 10,000) that's fine. If the user does
not, I want to insert one.

I tried the following code (user input is in variable $add_size):

if(ereg("^[0-9]+$", $add_size))    //if the user enters a numerical string
{ 
$add_size = strrev($add_size);
$add_size_02 = chunk_split($add_size,3,',');
$add_size_02 = strrev($add_size_02);
}

The idea being to reverse the string, insert a comma every three characters
and then reverse the string back before it is sent on it's merry way to the
query.

If I enter the string '12345'

after line one I get: 54321
after line two I get: 54,321
after line three I get: ,12,34

?!? Where does that new comma come from?

Thanks for any help!

-Jake

Message #2 by "Nikolai Devereaux" <yomama@u...> on Mon, 23 Sep 2002 13:28:42 -0700
Looking at the notes on the manual page for chunk_split, it appears that
chunk_split() adds the split character to the end of the string.  Using your
code, I get this output:

add size is  12345

reversed:    54321

chunk_split: 543,21,
reversed:    ,12,345

You just need to strip the extra comma.


Take care,

nik

p.s.  http://www.php.net/chunk_split

Message #3 by "Jacob Halpert" <jhalpert@m...> on Tue, 24 Sep 2002 01:51:19
> 
Looking at the notes on the manual page for chunk_split, it appears 
that
chunk_split() adds the split character to the end of the string.  Using 
your
code, I get this output:

add size is  12345

reversed:    54321

chunk_split: 543,21,
reversed:    ,12,345

You just need to strip the extra comma.


Take care,

nik

p.s.  http://www.php.net/chunk_split

Message #4 by "Jacob Halpert" <jhalpert@m...> on Tue, 24 Sep 2002 01:56:28
Of course! I  had MySQL set to accept only a six character string, so I 
never saw the last comma. A silly mistake, but one I overlooked for 
literally hours.

Thanks Nik 


> 
Looking at the notes on the manual page for chunk_split, it appears 
that
chunk_split() adds the split character to the end of the string.  Using 
your
code, I get this output:

add size is  12345

reversed:    54321

chunk_split: 543,21,
reversed:    ,12,345

You just need to strip the extra comma.


Take care,

nik

p.s.  http://www.php.net/chunk_split


  Return to Index