RE: [PHP] break up variable and put each element in an array
On Mon, 14 Aug 2006 19:14:42 -0500, Richard Lynch wrote: > On Mon, August 14, 2006 2:41 am, Ivo F.A.C. Fokkema wrote: >> On Sat, 12 Aug 2006 16:36:36 -0500, Richard Lynch wrote: >>> On Fri, August 11, 2006 3:11 am, Ivo F.A.C. Fokkema wrote: Well, if it's true that some browsers on some platforms ignore the W3C standard, I guess we could use: >>> >>> Or perhaps these browsers pre-date W3C standards. :-) >> >> Sure, but in newer versions, they might update their code to follow >> the standards after all... wouldn't they? > > Of course! > > But are all my users going to run out and upgrade because some W3C > weenie (from their persepctive) told them to? > > No. > > In fact, *some* of my users are so economically-challenged that their > hardware doesn't support a W3C-compliant browser due to memory > constraints. Holy sh*t! Seriously? Since it became a HTML specification back in 1999, that must be quite some old software. I just assumed people upgraded by then, so it wouldn't be a problem at all. Not too sure if it's the best "economically-challenged" way to go, since they're probably waiting for ever to get their system booted, but that's a different story. Do you happen to know their browser specs? I've went through the list of browsers visiting our website (based on JS statistics). These are the oldest, of every browser type, taken from the last 5000 page hits: Firefox 1.0 Mozilla 1.0 MS Internet Explorer 5.0 Netscape 7.0 Opera 7.5 Profile 1.0 Safari 1.2 WebTV/MSTV 2.6 MS IE 5.0 is pretty old :) And I don't even know 'Profile 1.0'... > So I'm going to provide backwards-compatible code which: > does not break anything for W3C-compliant browsers > supports ancient browsers > costs almost nothing in CPU time for any reasonable-sized GET/POST OK, OK, you won me over :) > Do you want to add to the Digital Divide by not supporting ancient > hardware/software, or do you want to only provide web services to the > wealthy :-) :-) :-) MUHAHAHAHAHH! Oh, sorry, was that out loud? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] break up variable and put each element in an array
On Mon, August 14, 2006 2:41 am, Ivo F.A.C. Fokkema wrote: > On Sat, 12 Aug 2006 16:36:36 -0500, Richard Lynch wrote: >> On Fri, August 11, 2006 3:11 am, Ivo F.A.C. Fokkema wrote: >>> Well, if it's true that some browsers on some platforms ignore the >>> W3C >>> standard, I guess we could use: >> >> Or perhaps these browsers pre-date W3C standards. :-) > > Sure, but in newer versions, they might update their code to follow > the standards after all... wouldn't they? Of course! But are all my users going to run out and upgrade because some W3C weenie (from their persepctive) told them to? No. In fact, *some* of my users are so economically-challenged that their hardware doesn't support a W3C-compliant browser due to memory constraints. So I'm going to provide backwards-compatible code which: does not break anything for W3C-compliant browsers supports ancient browsers costs almost nothing in CPU time for any reasonable-sized GET/POST Do you want to add to the Digital Divide by not supporting ancient hardware/software, or do you want to only provide web services to the wealthy :-) :-) :-) -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] break up variable and put each element in an array
On Sat, 12 Aug 2006 16:36:36 -0500, Richard Lynch wrote: > On Fri, August 11, 2006 3:11 am, Ivo F.A.C. Fokkema wrote: >> Well, if it's true that some browsers on some platforms ignore the W3C >> standard, I guess we could use: > > Or perhaps these browsers pre-date W3C standards. :-) Sure, but in newer versions, they might update their code to follow the standards after all... wouldn't they? >> $datelist = >> str_replace(array("\r\n","\n","\r"),'',$_POST['datelist']); > > Two problems here. > > This assumes a specific undocumented ordering to the replacement in > processing the array argument. If, for some insane reason, the PHP > implementation of str_replace chooses to process that input array in > reverse order, you would end up with double for "\r\n" input. This > is incredibly unlikely, but it's NOT (yet) documented that the arrays > are processed in order. True; and I haven't tested the code either, to be honest. I found the basic implementation on a forum somewhere and adapted it to the threadstarter's need. I might try and see if I could submit a documentation bug on this. I think it's a useful feature and you're right to claim that since it's not documented, one should not rely on it. > The next problem is that replacing newline (in any form) with on > the INPUT phase of your program is just a Bad Idea. :-) > > (...) > > Because someday you may want to output that same data to RSS, XML, or > XYZ, in which case is NOT what you want for your newlines. Hey, totally agree here, BUT, the threadstarter doesn't mention it being put in a database. Actually, he just requests it to be written on a new line in the output. I just provided him a way of doing this. I always store the input directly in the database (quoted, then, of course) and replace \r\n with when needed (display on screen). I've had no problems by strictly using \r\n yet. But as I have an open source project, I assume someone will (be telling me||submit a bug) if they have problems. In that case, I will implement a non-W3C compliant hack ;) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] break up variable and put each element in an array
On Fri, August 11, 2006 3:11 am, Ivo F.A.C. Fokkema wrote: > Well, if it's true that some browsers on some platforms ignore the W3C > standard, I guess we could use: Or perhaps these browsers pre-date W3C standards. :-) > $datelist = > str_replace(array("\r\n","\n","\r"),'',$_POST['datelist']); Two problems here. This assumes a specific undocumented ordering to the replacement in processing the array argument. If, for some insane reason, the PHP implementation of str_replace chooses to process that input array in reverse order, you would end up with double for "\r\n" input. This is incredibly unlikely, but it's NOT (yet) documented that the arrays are processed in order. The next problem is that replacing newline (in any form) with on the INPUT phase of your program is just a Bad Idea. :-) The INPUT data should be kept sacrosanct and the OS-dependent newlines should be converted to newlines. If the data is output later to a browser, nl2br() should be used. Because someday you may want to output that same data to RSS, XML, or XYZ, in which case is NOT what you want for your newlines. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] break up variable and put each element in an array
On Fri, August 11, 2006 2:40 am, Reinhart Viane wrote: > Actually, I know that it's browser/OS dependent, cuz I had a bunch of > Mac users who sent only \r all the time. > > This may be true only of OS 9, and you may not care about them > anymore, but there it is. > > I also would not be so quick to claim that Linux sends \r\n -- It > could be dependent on the browser, the OS version, the OS distro, some > OS settings, ... > > Better safe than sorry, and I *know* I ran into this with some Mac > users. > > Plus I hate trying to edit the text chunks in vi with those icky \r > thingies that turn into ^M :-) > > What would you suggest to use then? I would suggest using the code I posted that started this sub-thread... :-) //The order of operations is crucual here: $string = str_replace("\r\n", "\n", $string); $string = str_replace("\r", "\n", $string); You could use arrays for str_replace, and reduce the function calls, but that relies on the ORDER in which the array/substitutions are processed. While I can't imagine the array version of str_replace doing them in reverse order, it is not currently a documented behaviour, so I personally would just take the penalty of 2 function calls, unless it's a proven performance problem (which seems unlikely). YMMV -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] break up variable and put each element in an array
On Thu, 10 Aug 2006 17:17:23 -0500, Richard Lynch wrote: > On Tue, August 8, 2006 3:47 pm, Fokkema, I.F.A.C. \(HKG\) wrote: If the user separates the dates by an enter in the textarea, you need to explode on "\r\n". To be able to handle both, you need to use split() or preg_split(). When I use $datelist=$_POST[datelist]; $string=explode('\r\n',$datelist); >>> >>> '\r\n' and "\r\n" are not anything alike... >>> >>> \r\n only have special meaning inside of ", not ' >>> >>> If you want to cover all browser/OS combinations, you'd want to >>> pre-treat the text area input like: >>> $datelist = $_POST['datelist']; >>> $datelist = str_replace("\r\n", "\r", $datelist); >>> $datelist = str_replace("\r", "\n", $datelist); >>> >>> Now all the date are separated by "\n" and you can reliably use >>> explode("\n", $datelist); on them. >> >> Actually, I know that both Windows and Linux send a linebreak in a >> textarea as "\r\n", not "\n". I assume it's all the same for all >> platforms, and that Mac would send "\r\n", too. > > Actually, I know that it's browser/OS dependent, cuz I had a bunch of > Mac users who sent only \r all the time. > > This may be true only of OS 9, and you may not care about them > anymore, but there it is. > > I also would not be so quick to claim that Linux sends \r\n -- It > could be dependent on the browser, the OS version, the OS distro, some > OS settings, ... I've tested this on different distros and browsers, all had sent \r\n and in years I've not had any problems with not receiving \r\n. But I did some extensive searching, and I found this in the W3C specifications: "Line breaks, as in multi-line text field values, are represented as "CR LF" pairs, i.e., `%0D%0A'." (http://www.w3.org/TR/WD-html40-970917/interact/forms.html) > Better safe than sorry, and I *know* I ran into this with some Mac users. > > Plus I hate trying to edit the text chunks in vi with those icky \r > thingies that turn into ^M :-) Well, if it's true that some browsers on some platforms ignore the W3C standard, I guess we could use: $datelist = str_replace(array("\r\n","\n","\r"),'',$_POST['datelist']); Ivo -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] break up variable and put each element in an array
Actually, I know that it's browser/OS dependent, cuz I had a bunch of Mac users who sent only \r all the time. This may be true only of OS 9, and you may not care about them anymore, but there it is. I also would not be so quick to claim that Linux sends \r\n -- It could be dependent on the browser, the OS version, the OS distro, some OS settings, ... Better safe than sorry, and I *know* I ran into this with some Mac users. Plus I hate trying to edit the text chunks in vi with those icky \r thingies that turn into ^M :-) What would you suggest to use then? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] break up variable and put each element in an array
On Tue, August 8, 2006 3:47 pm, Fokkema, I.F.A.C. \(HKG\) wrote: try this: $string = "3/01/2005 29/12/2005 2/01/2006 20/02/2006 28/12/2006 1/01/2007 15/02/2007"; $array = explode(' ', $string); foreach ($array as $value) echo "Date: $value"; >>> >>> If the user separates the dates by an enter in the textarea, you >>> need >>> to >>> explode on "\r\n". To be able to handle both, you need to use >>> split() >>> or >>> preg_split(). >>> >>> >>> When I use >>> $datelist=$_POST[datelist]; >>> $string=explode('\r\n',$datelist); >> >> '\r\n' and "\r\n" are not anything alike... >> >> \r\n only have special meaning inside of ", not ' >> >> If you want to cover all browser/OS combinations, you'd want to >> pre-treat the text area input like: >> $datelist = $_POST['datelist']; >> $datelist = str_replace("\r\n", "\r", $datelist); >> $datelist = str_replace("\r", "\n", $datelist); >> >> Now all the date are separated by "\n" and you can reliably use >> explode("\n", $datelist); on them. > > Actually, I know that both Windows and Linux send a linebreak in a > textarea as "\r\n", not "\n". I assume it's all the same for all > platforms, and that Mac would send "\r\n", too. Actually, I know that it's browser/OS dependent, cuz I had a bunch of Mac users who sent only \r all the time. This may be true only of OS 9, and you may not care about them anymore, but there it is. I also would not be so quick to claim that Linux sends \r\n -- It could be dependent on the browser, the OS version, the OS distro, some OS settings, ... Better safe than sorry, and I *know* I ran into this with some Mac users. Plus I hate trying to edit the text chunks in vi with those icky \r thingies that turn into ^M :-) -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] break up variable and put each element in an array
>>> try this: >>> >>> $string = "3/01/2005 29/12/2005 2/01/2006 20/02/2006 28/12/2006 >>> 1/01/2007 >>> 15/02/2007"; >>> $array = explode(' ', $string); >>> foreach ($array as $value) echo "Date: $value"; >> >> If the user separates the dates by an enter in the textarea, you need >> to >> explode on "\r\n". To be able to handle both, you need to use split() >> or >> preg_split(). >> >> >> When I use >> $datelist=$_POST[datelist]; >> $string=explode('\r\n',$datelist); > > '\r\n' and "\r\n" are not anything alike... > > \r\n only have special meaning inside of ", not ' > > If you want to cover all browser/OS combinations, you'd want to > pre-treat the text area input like: > $datelist = $_POST['datelist']; > $datelist = str_replace("\r\n", "\r", $datelist); > $datelist = str_replace("\r", "\n", $datelist); > > Now all the date are separated by "\n" and you can reliably use > explode("\n", $datelist); on them. Actually, I know that both Windows and Linux send a linebreak in a textarea as "\r\n", not "\n". I assume it's all the same for all platforms, and that Mac would send "\r\n", too.
RE: [PHP] break up variable and put each element in an array
On Tue, August 8, 2006 9:06 am, Reinhart Viane wrote: >> try this: >> >> $string = "3/01/2005 29/12/2005 2/01/2006 20/02/2006 28/12/2006 >> 1/01/2007 >> 15/02/2007"; >> $array = explode(' ', $string); >> foreach ($array as $value) echo "Date: $value"; > > If the user separates the dates by an enter in the textarea, you need > to > explode on "\r\n". To be able to handle both, you need to use split() > or > preg_split(). > > > When I use > $datelist=$_POST[datelist]; > $string=explode('\r\n',$datelist); '\r\n' and "\r\n" are not anything alike... \r\n only have special meaning inside of ", not ' If you want to cover all browser/OS combinations, you'd want to pre-treat the text area input like: $datelist = $_POST['datelist']; $datelist = str_replace("\r\n", "\r", $datelist); $datelist = str_replace("\r", "\n", $datelist); Now all the date are separated by "\n" and you can reliably use explode("\n", $datelist); on them. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] break up variable and put each element in an array
On Tue, August 8, 2006 8:02 am, Reinhart Viane wrote: > A. I have a page on which people can supply dates in a text area. > Dates are > entered like this: > 3/01/2005 > 29/12/2005 > 2/01/2006 > 20/02/2006 > 28/12/2006 > 1/01/2007 > 15/02/2007 > > B. Now I need this Post element to be broken into pieces (per date) > and each > of those pieces should be put into a text so the outcome (echo on > screen) > would be like this: > Date=3/01/2005 > Date=29/12/2005 > Date=2/01/2006 > Date=20/02/2006 > Date=28/12/2006 > Date=1/01/2007 > Date=15/02/2007 $dates = "Date=" . implode("\nDate=", explode("\n", $dates); -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] break up variable and put each element in an array
Thx all -Oorspronkelijk bericht- Van: Ivo F.A.C. Fokkema [mailto:[EMAIL PROTECTED] Verzonden: dinsdag 8 augustus 2006 16:17 Aan: Reinhart Viane CC: php-general@lists.php.net Onderwerp: RE: [PHP] break up variable and put each element in an array On Tue, 2006-08-08 at 16:06 +0200, Reinhart Viane wrote: > > try this: > > > > $string = "3/01/2005 29/12/2005 2/01/2006 20/02/2006 28/12/2006 1/01/2007 > > 15/02/2007"; > > $array = explode(' ', $string); > > foreach ($array as $value) echo "Date: $value"; > > If the user separates the dates by an enter in the textarea, you need to > explode on "\r\n". To be able to handle both, you need to use split() or > preg_split(). > > > When I use > $datelist=$_POST[datelist]; > $string=explode('\r\n',$datelist); > echo $string; > foreach ($string as $value) { > echo "Date: $value\n"; > > this is what I get: > ArrayDate: 24/12/2018 26/12/2018 31/12/2018 14/02/2019 14/03/2019 20/06/2019 > 24/06/2019 26/06/2019 27/06/2019 27/06/2019 You'll need to use "\r\n", with double quotes. Single quotes don't interpret the \r\n the same. > This solved the issue, although I find it rather bizar: > $datelist=$_POST[datelist]; > $string=preg_split('/\r\n/', $datelist, -1, PREG_SPLIT_OFFSET_CAPTURE); > echo $string; > foreach ($string as $value) { >echo "Date: $value\n"; > } > > Btw the input is a copy/paste from within a program > Thx all You might also want to try: $string=preg_split('/\s+/', $datelist); which would split on any (one or more) whitespace character(s). -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] break up variable and put each element in an array
> try this: > > $string = "3/01/2005 29/12/2005 2/01/2006 20/02/2006 28/12/2006 1/01/2007 > 15/02/2007"; > $array = explode(' ', $string); > foreach ($array as $value) echo "Date: $value"; If the user separates the dates by an enter in the textarea, you need to explode on "\r\n". To be able to handle both, you need to use split() or preg_split(). When I use $datelist=$_POST[datelist]; $string=explode('\r\n',$datelist); echo $string; foreach ($string as $value) { echo "Date: $value\n"; this is what I get: ArrayDate: 24/12/2018 26/12/2018 31/12/2018 14/02/2019 14/03/2019 20/06/2019 24/06/2019 26/06/2019 27/06/2019 27/06/2019 This solved the issue, although I find it rather bizar: $datelist=$_POST[datelist]; $string=preg_split('/\r\n/', $datelist, -1, PREG_SPLIT_OFFSET_CAPTURE); echo $string; foreach ($string as $value) { echo "Date: $value\n"; } Btw the input is a copy/paste from within a program Thx all -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] break up variable and put each element in an array
On Tue, 2006-08-08 at 16:06 +0200, Reinhart Viane wrote: > > try this: > > > > $string = "3/01/2005 29/12/2005 2/01/2006 20/02/2006 28/12/2006 1/01/2007 > > 15/02/2007"; > > $array = explode(' ', $string); > > foreach ($array as $value) echo "Date: $value"; > > If the user separates the dates by an enter in the textarea, you need to > explode on "\r\n". To be able to handle both, you need to use split() or > preg_split(). > > > When I use > $datelist=$_POST[datelist]; > $string=explode('\r\n',$datelist); > echo $string; > foreach ($string as $value) { > echo "Date: $value\n"; > > this is what I get: > ArrayDate: 24/12/2018 26/12/2018 31/12/2018 14/02/2019 14/03/2019 20/06/2019 > 24/06/2019 26/06/2019 27/06/2019 27/06/2019 You'll need to use "\r\n", with double quotes. Single quotes don't interpret the \r\n the same. > This solved the issue, although I find it rather bizar: > $datelist=$_POST[datelist]; > $string=preg_split('/\r\n/', $datelist, -1, PREG_SPLIT_OFFSET_CAPTURE); > echo $string; > foreach ($string as $value) { >echo "Date: $value\n"; > } > > Btw the input is a copy/paste from within a program > Thx all You might also want to try: $string=preg_split('/\s+/', $datelist); which would split on any (one or more) whitespace character(s). -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] break up variable and put each element in an array
On Tue, 08 Aug 2006 14:50:53 +0100, Dave Goodchild wrote: > On 08/08/06, Reinhart Viane <[EMAIL PROTECTED]> wrote: >> >> A. I have a page on which people can supply dates in a text area. Dates >> are >> entered like this: >> 3/01/2005 >> 29/12/2005 >> 2/01/2006 >> 20/02/2006 >> 28/12/2006 >> 1/01/2007 >> 15/02/2007 >> >> B. Now I need this Post element to be broken into pieces (per date) and >> each >> of those pieces should be put into a text so the outcome (echo on screen) >> would be like this: >> Date=3/01/2005 >> Date=29/12/2005 >> Date=2/01/2006 >> Date=20/02/2006 >> Date=28/12/2006 >> Date=1/01/2007 >> Date=15/02/2007 >> >> The posted variable from A looks like this (when written on screen): >> 3/01/2005 29/12/2005 2/01/2006 20/02/2006 28/12/2006 1/01/2007 15/02/2007 >> >> So I need break this up and store any individual date into a key of an >> array >> Afterwards I need to loop through this array and use the value of each key >> to be printed after 'Date=' >> > > try this: > > $string = "3/01/2005 29/12/2005 2/01/2006 20/02/2006 28/12/2006 1/01/2007 > 15/02/2007"; > $array = explode(' ', $string); > foreach ($array as $value) echo "Date: $value"; If the user separates the dates by an enter in the textarea, you need to explode on "\r\n". To be able to handle both, you need to use split() or preg_split(). -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] break up variable and put each element in an array
On 08/08/06, Reinhart Viane <[EMAIL PROTECTED]> wrote: A. I have a page on which people can supply dates in a text area. Dates are entered like this: 3/01/2005 29/12/2005 2/01/2006 20/02/2006 28/12/2006 1/01/2007 15/02/2007 B. Now I need this Post element to be broken into pieces (per date) and each of those pieces should be put into a text so the outcome (echo on screen) would be like this: Date=3/01/2005 Date=29/12/2005 Date=2/01/2006 Date=20/02/2006 Date=28/12/2006 Date=1/01/2007 Date=15/02/2007 The posted variable from A looks like this (when written on screen): 3/01/2005 29/12/2005 2/01/2006 20/02/2006 28/12/2006 1/01/2007 15/02/2007 So I need break this up and store any individual date into a key of an array Afterwards I need to loop through this array and use the value of each key to be printed after 'Date=' try this: $string = "3/01/2005 29/12/2005 2/01/2006 20/02/2006 28/12/2006 1/01/2007 15/02/2007"; $array = explode(' ', $string); foreach ($array as $value) echo "Date: $value";