Re: [PHP] Strange string stuff -- maybe everything is ending...
On Fri, Dec 21, 2012 at 7:06 PM, Jim Giner jim.gi...@albanyhandball.com wrote: That actually makes sense tho. Afterall, a string is truly only one memory allocation whereas array elements are basically multiple vars having the same name. So - how can you unset one char in a string? That actually depends on what you mean by unset. If you mean to remove it, str_replace should do, or if you only know it's position, using substr twice to grab the front and back should work. Setting a charcter to null ('') in the middle of the string could be interesting for some applications... Following up with the example I just showed: // now let's 'unset' a character in the middle $s3 = $s; $s3[10] = ''; showstring($s3); yields: String of length 34 is Now we aregone down to the river! s[0] : 'N' 78 s[1] : 'o' 111 s[2] : 'w' 119 s[3] : ' ' 32 s[4] : 'w' 119 s[5] : 'e' 101 s[6] : ' ' 32 s[7] : 'a' 97 s[8] : 'r' 114 s[9] : 'e' 101 s[10] : '' 0 s[11] : 'g' 103 s[12] : 'o' 111 s[13] : 'n' 110 s[14] : 'e' 101 s[15] : ' ' 32 s[16] : 'd' 100 s[17] : 'o' 111 s[18] : 'w' 119 s[19] : 'n' 110 s[20] : ' ' 32 s[21] : 't' 116 s[22] : 'o' 111 s[23] : ' ' 32 s[24] : 't' 116 s[25] : 'h' 104 s[26] : 'e' 101 s[27] : ' ' 32 s[28] : 'r' 114 s[29] : 'i' 105 s[30] : 'v' 118 s[31] : 'e' 101 s[32] : 'r' 114 s[33] : '!' 33 Still 34 byte string length, but looking at s[10] you see the null byte. It didn't really affect printing, but it might affect other things. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strange string stuff -- maybe everything is ending...
On Dec 21, 2012, at 5:20 PM, Volmar Machado qi.vol...@gmail.com wrote: What is the result in FF? And on IE? (the echoed string) That's the problem, it's different. If the last char in a string is set to null, then it causes JavaScript routines running under IE to behave differently than the exact same JavaScript routines running under Safari or FireFox. This was something I never expected. However, the web industry has had to deal with IE oddities for many, many years -- the problem remains and it's called IE. Remember, M$ always has a better idea. Cheers, tedd _ t...@sperling.com http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strange string stuff -- maybe everything is ending...
On 12/22/2012 11:29 AM, Tedd Sperling wrote: On Dec 21, 2012, at 5:20 PM, Volmar Machado qi.vol...@gmail.com wrote: What is the result in FF? And on IE? (the echoed string) That's the problem, it's different. If the last char in a string is set to null, then it causes JavaScript routines running under IE to behave differently than the exact same JavaScript routines running under Safari or FireFox. This was something I never expected. However, the web industry has had to deal with IE oddities for many, many years -- the problem remains and it's called IE. Remember, M$ always has a better idea. Cheers, tedd _ t...@sperling.com http://sperling.com But basically, I think the work that others have done shows you that your use of indices on strings is not the best way to manipulate them. Hey - it's a string - use substr. :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strange string stuff -- maybe everything is ending...
On Dec 21, 2012, at 5:27 PM, Jim Giner jim.gi...@albanyhandball.com wrote: From what I do know, there shouldn't be an a[4]. In any case, let's assume that there is a bug in the string logic that you're using. Why not just use substr? $topic = substr($topic,0,-1); and On Dec 21, 2012, at 6:10 PM, Nathan Nobbe quickshif...@gmail.com wrote: Neat idea Tedd, but judging by a quick test, I don't think changing the value of the string is entirely supported though that notation. php $str = 'blah'; php $str[3] = ''; php echo $str . PHP_EOL; bla php echo strlen($str); 4 I'm not looking for a solution, but rather pointing out something I never encountered before. I would have never thought that a string echoed by a PHP script to be used in a JavaScript routine would depend upon what Browser it is run on. That seems odd. Cheers, tedd _ t...@sperling.com http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strange string stuff -- maybe everything is ending...
On Dec 21, 2012, at 8:06 PM, Jim Giner jim.gi...@albanyhandball.com wrote: That actually makes sense tho. Afterall, a string is truly only one memory allocation whereas array elements are basically multiple vars having the same name. So - how can you unset one char in a string? It depends upon the language -- while it is true that the start of a string is located at a memory address, the chars of the string are identical to the chars in an array. As such, you can view a string as an array. Each index is representative of a char (one byte) in the string. Cheers, tedd _ t...@sperling.com http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strange string stuff -- maybe everything is ending...
On Dec 22, 2012, at 7:58 AM, tamouse mailing lists tamouse.li...@gmail.com wrote: A bit of an example to shed a little light? -snip- Not knowing IE really at all, nor it's JS engine, it's entirely possible that a null character in a string causes it to have problems. That's the explanation I was looking for -- thanks! Cheers, tedd _ t...@sperling.com http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strange string stuff -- maybe everything is ending...
Bastien Koert On 2012-12-22, at 11:50 AM, Tedd Sperling t...@sperling.com wrote: On Dec 22, 2012, at 7:58 AM, tamouse mailing lists tamouse.li...@gmail.com wrote: A bit of an example to shed a little light? -snip- Not knowing IE really at all, nor it's JS engine, it's entirely possible that a null character in a string causes it to have problems. That's the explanation I was looking for -- thanks! You'll need to check various versions of IE. IE8 has significant JS speed and performance issues that don't show up in v7 or v9. God bless Internet Exploder Cheers, tedd _ t...@sperling.com http://sperling.com -- 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
Re: [PHP] Strange string stuff -- maybe everything is ending...
On 22.12.2012 00:31, Bastien wrote: On 2012-12-21, at 5:05 PM, Ken Robinson kenrb...@rbnsn.com wrote: A much easier way to do this would be to use a temporary array and then explode: $tmp = array(); while($row = mysql_fetch_array($result)) // pulling stuff from a database { $tmp[] = $row['category']; } $topic = explode('~',$tmp); // put the delimiter between each entry echo($topic); // this result is used in an AJAX script Shouldn't that be implode to convert the array to string with the delimiter? Bastien Yes, it should be implode(). Stefan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strange string stuff -- maybe everything is ending...
A much easier way to do this would be to use a temporary array and then explode: $tmp = array(); while($row = mysql_fetch_array($result)) // pulling stuff from a database { $tmp[] = $row['category']; } $topic = explode('~',$tmp); // put the delimiter between each entry echo($topic); // this result is used in an AJAX script No more worrying about whether the delimiter is the last character. Ken At 04:38 PM 12/21/2012, Tedd Sperling wrote: Hi gang; I just ran into something I have never had a problem with before. Here's the code: --- start of code $topic = ''; while($row = mysql_fetch_array($result)) // pulling stuff from a database { $topic .= $row['category'] . '~'; // adding a delimiter between categories } $str_length = strlen($topic); if($topic[$str_length-1] == '~') { $topic[$str_length-1] = ''; // remove last ~ delimiter } echo($topic); // this result is used in an AJAX script --- end of code Now, when the result is displayed (i.e., echoed) to Javascript routines running in Safari and FireFox, everything is OK. But when the result is displayed on IE, the end of the string causes problems with the exact same javascript routine as used above. Now, I realize that I have altered the string by removing the last character and I have not shortened the string to reflect that, but I never thought it would cause any problems. Would someone please enlighten me as to why this would work in Safari, FireFox, but not in IE? Cheers, tedd PS: Also, please don't beg the answer by saying It's IE -- what do you expect? _ t...@sperling.com http://sperling.com -- 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
Re: [PHP] Strange string stuff -- maybe everything is ending...
On Dec 21, 2012, at 5:05 PM, Ken Robinson kenrb...@rbnsn.com wrote A much easier way to do this would be to use a temporary array and then explode: $tmp = array(); while($row = mysql_fetch_array($result)) // pulling stuff from a database { $tmp[] = $row['category']; } $topic = explode('~',$tmp); // put the delimiter between each entry echo($topic); // this result is used in an AJAX script No more worrying about whether the delimiter is the last character. Ken Ken: Slick -- I like it. But, the question remains -- if you make the last character in a string a '' (or null), then what's the problem with doing that? Cheers, tedd _ t...@sperling.com http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strange string stuff -- maybe everything is ending...
On Dec 21, 2012, at 4:58 PM, Jim Giner jim.gi...@albanyhandball.com wrote: Never realized that you could address a string as an array of chars, which you are doing. Could that be the issue? Or did I learn something new? Or should you have used substr to remove that last char? Jim: I guess you learned something new -- that's good. A string is just a string of chars. As such, if you define: $a = tedd; then: $a[0] is 't'; $a[1] is 'e' $a[2] is 'd' $a[3] is 'd' The only confusing thing here is the length of the string -- in this case the length of this string is four, but $a[4] has not been defined. Cheers, tedd _ t...@sperling.com http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strange string stuff -- maybe everything is ending...
Can you post the javascript that's causing the problem? And, yes, it's IE, what did you expect... :-) Ken At 05:10 PM 12/21/2012, Tedd Sperling wrote: On Dec 21, 2012, at 5:05 PM, Ken Robinson kenrb...@rbnsn.com wrote A much easier way to do this would be to use a temporary array and then explode: $tmp = array(); while($row = mysql_fetch_array($result)) // pulling stuff from a database { $tmp[] = $row['category']; } $topic = explode('~',$tmp); // put the delimiter between each entry echo($topic); // this result is used in an AJAX script No more worrying about whether the delimiter is the last character. Ken Ken: Slick -- I like it. But, the question remains -- if you make the last character in a string a '' (or null), then what's the problem with doing that? Cheers, tedd _ t...@sperling.com http://sperling.com -- 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
Re: [PHP] Strange string stuff -- maybe everything is ending...
On 12/21/2012 5:16 PM, Tedd Sperling wrote: On Dec 21, 2012, at 4:58 PM, Jim Giner jim.gi...@albanyhandball.com wrote: Never realized that you could address a string as an array of chars, which you are doing. Could that be the issue? Or did I learn something new? Or should you have used substr to remove that last char? Jim: I guess you learned something new -- that's good. A string is just a string of chars. As such, if you define: $a = tedd; then: $a[0] is 't'; $a[1] is 'e' $a[2] is 'd' $a[3] is 'd' The only confusing thing here is the length of the string -- in this case the length of this string is four, but $a[4] has not been defined. Cheers, tedd _ t...@sperling.com http://sperling.com From what I do know, there shouldn't be an a[4]. In any case, let's assume that there is a bug in the string logic that you're using. Why not just use substr? $topic = substr($topic,0,-1); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strange string stuff -- maybe everything is ending...
On Fri, Dec 21, 2012 at 3:27 PM, Jim Giner jim.gi...@albanyhandball.comwrote: On 12/21/2012 5:16 PM, Tedd Sperling wrote: On Dec 21, 2012, at 4:58 PM, Jim Giner jim.gi...@albanyhandball.com wrote: Never realized that you could address a string as an array of chars, which you are doing. Could that be the issue? Or did I learn something new? Or should you have used substr to remove that last char? Jim: I guess you learned something new -- that's good. A string is just a string of chars. As such, if you define: $a = tedd; then: $a[0] is 't'; $a[1] is 'e' $a[2] is 'd' $a[3] is 'd' The only confusing thing here is the length of the string -- in this case the length of this string is four, but $a[4] has not been defined. Cheers, tedd _ t...@sperling.com http://sperling.com From what I do know, there shouldn't be an a[4]. In any case, let's assume that there is a bug in the string logic that you're using. Why not just use substr? $topic = substr($topic,0,-1); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Neat idea Tedd, but judging by a quick test, I don't think changing the value of the string is entirely supported though that notation. php $str = 'blah'; php $str[3] = ''; php echo $str . PHP_EOL; bla php echo strlen($str); 4 -nathan
Re: [PHP] Strange string stuff -- maybe everything is ending...
On 12/21/2012 6:10 PM, Nathan Nobbe wrote: On Fri, Dec 21, 2012 at 3:27 PM, Jim Giner jim.gi...@albanyhandball.comwrote: Neat idea Tedd, but judging by a quick test, I don't think changing the value of the string is entirely supported though that notation. php $str = 'blah'; php $str[3] = ''; php echo $str . PHP_EOL; bla php echo strlen($str); 4 -nathan Aha!!! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strange string stuff -- maybe everything is ending...
Think that PHP inherited this from the C-language. The C-language inherited this from good old assembler :-) Index of the array is the offset from the starting-address of the string. Suppose the $a starts at address 1000 than $a[0] is at 1000 + 0, $a[1] is at 1000 + 1 $a[2] is at 1000 + 2 etc The fact that $a[4] does not exist makes this indeed confusing, perhaps PHP could have solved this internally ? Gr. Louis On 12/21/12 23:27, Jim Giner wrote: On 12/21/2012 5:16 PM, Tedd Sperling wrote: On Dec 21, 2012, at 4:58 PM, Jim Giner jim.gi...@albanyhandball.com wrote: Never realized that you could address a string as an array of chars, which you are doing. Could that be the issue? Or did I learn something new? Or should you have used substr to remove that last char? Jim: I guess you learned something new -- that's good. A string is just a string of chars. As such, if you define: $a = tedd; then: $a[0] is 't'; $a[1] is 'e' $a[2] is 'd' $a[3] is 'd' The only confusing thing here is the length of the string -- in this case the length of this string is four, but $a[4] has not been defined. Cheers, tedd _ t...@sperling.com http://sperling.com From what I do know, there shouldn't be an a[4]. In any case, let's assume that there is a bug in the string logic that you're using. Why not just use substr? $topic = substr($topic,0,-1); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strange string stuff -- maybe everything is ending...
On 2012-12-21, at 5:05 PM, Ken Robinson kenrb...@rbnsn.com wrote: A much easier way to do this would be to use a temporary array and then explode: $tmp = array(); while($row = mysql_fetch_array($result)) // pulling stuff from a database { $tmp[] = $row['category']; } $topic = explode('~',$tmp); // put the delimiter between each entry echo($topic); // this result is used in an AJAX script Shouldn't that be implode to convert the array to string with the delimiter? Bastien -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strange string stuff -- maybe everything is ending...
On Fri, Dec 21, 2012 at 4:10 PM, Nathan Nobbe quickshif...@gmail.comwrote: On Fri, Dec 21, 2012 at 3:27 PM, Jim Giner jim.gi...@albanyhandball.comwrote: On 12/21/2012 5:16 PM, Tedd Sperling wrote: On Dec 21, 2012, at 4:58 PM, Jim Giner jim.gi...@albanyhandball.com wrote: Never realized that you could address a string as an array of chars, which you are doing. Could that be the issue? Or did I learn something new? Or should you have used substr to remove that last char? Jim: I guess you learned something new -- that's good. A string is just a string of chars. As such, if you define: $a = tedd; then: $a[0] is 't'; $a[1] is 'e' $a[2] is 'd' $a[3] is 'd' The only confusing thing here is the length of the string -- in this case the length of this string is four, but $a[4] has not been defined. Cheers, tedd _ t...@sperling.com http://sperling.com From what I do know, there shouldn't be an a[4]. In any case, let's assume that there is a bug in the string logic that you're using. Why not just use substr? $topic = substr($topic,0,-1); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Neat idea Tedd, but judging by a quick test, I don't think changing the value of the string is entirely supported though that notation. php $str = 'blah'; php $str[3] = ''; php echo $str . PHP_EOL; bla php echo strlen($str); 4 Another interesting twist along the same lines, seems the string offsets are indeed read only: php unset($str[3]); Fatal error: Cannot unset string offsets in php shell code on line 1 Call Stack: 6665.6475 384568 1. {main}() php shell code:0 -nathan
Re: [PHP] Strange string stuff -- maybe everything is ending...
On 12/21/2012 7:59 PM, Nathan Nobbe wrote: On Fri, Dec 21, 2012 at 4:10 PM, Nathan Nobbe quickshif...@gmail.comwrote: On Fri, Dec 21, 2012 at 3:27 PM, Jim Giner jim.gi...@albanyhandball.comwrote: On 12/21/2012 5:16 PM, Tedd Sperling wrote: On Dec 21, 2012, at 4:58 PM, Jim Giner jim.gi...@albanyhandball.com wrote: Never realized that you could address a string as an array of chars, which you are doing. Could that be the issue? Or did I learn something new? Or should you have used substr to remove that last char? Jim: I guess you learned something new -- that's good. A string is just a string of chars. As such, if you define: $a = tedd; then: $a[0] is 't'; $a[1] is 'e' $a[2] is 'd' $a[3] is 'd' The only confusing thing here is the length of the string -- in this case the length of this string is four, but $a[4] has not been defined. Cheers, tedd _ t...@sperling.com http://sperling.com From what I do know, there shouldn't be an a[4]. In any case, let's assume that there is a bug in the string logic that you're using. Why not just use substr? $topic = substr($topic,0,-1); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Neat idea Tedd, but judging by a quick test, I don't think changing the value of the string is entirely supported though that notation. php $str = 'blah'; php $str[3] = ''; php echo $str . PHP_EOL; bla php echo strlen($str); 4 Another interesting twist along the same lines, seems the string offsets are indeed read only: php unset($str[3]); Fatal error: Cannot unset string offsets in php shell code on line 1 Call Stack: 6665.6475 384568 1. {main}() php shell code:0 -nathan That actually makes sense tho. Afterall, a string is truly only one memory allocation whereas array elements are basically multiple vars having the same name. So - how can you unset one char in a string? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php