php-general Digest 22 Dec 2012 12:58:06 -0000 Issue 8073
Topics (messages 319923 through 319935):
Re: Strange string stuff -- maybe everything is ending...
319923 by: Jim Giner
319924 by: Ken Robinson
319925 by: Tedd Sperling
319926 by: Tedd Sperling
319927 by: Ken Robinson
319928 by: Jim Giner
319929 by: Nathan Nobbe
319930 by: Jim Giner
319931 by: Louis Colman
319932 by: Bastien
319933 by: Nathan Nobbe
319934 by: Jim Giner
319935 by: tamouse mailing lists
Administrivia:
To subscribe to the digest, e-mail:
php-general-digest-subscr...@lists.php.net
To unsubscribe from the digest, e-mail:
php-general-digest-unsubscr...@lists.php.net
To post to the list, e-mail:
php-gene...@lists.php.net
----------------------------------------------------------------------
--- Begin Message ---
On 12/21/2012 4:38 PM, 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
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?
--- End Message ---
--- Begin Message ---
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
--- End Message ---
--- Begin Message ---
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
--- End Message ---
--- Begin Message ---
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
--- End Message ---
--- Begin Message ---
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
--- End Message ---
--- Begin Message ---
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);
--- End Message ---
--- Begin Message ---
On Fri, Dec 21, 2012 at 3:27 PM, Jim Giner <jim.gi...@albanyhandball.com>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
>
>
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
--- End Message ---
--- Begin Message ---
On 12/21/2012 6:10 PM, Nathan Nobbe wrote:
On Fri, Dec 21, 2012 at 3:27 PM, Jim Giner <jim.gi...@albanyhandball.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
-nathan
Aha!!!
--- End Message ---
--- Begin Message ---
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);
--- End Message ---
--- Begin Message ---
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
--- End Message ---
--- Begin Message ---
On Fri, Dec 21, 2012 at 4:10 PM, Nathan Nobbe <quickshif...@gmail.com>wrote:
>
>
> On Fri, Dec 21, 2012 at 3:27 PM, Jim Giner
> <jim.gi...@albanyhandball.com>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
>>
>>
> 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
--- End Message ---
--- Begin Message ---
On 12/21/2012 7:59 PM, Nathan Nobbe wrote:
On Fri, Dec 21, 2012 at 4:10 PM, Nathan Nobbe <quickshif...@gmail.com>wrote:
On Fri, Dec 21, 2012 at 3:27 PM, Jim Giner <jim.gi...@albanyhandball.com>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
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?
--- End Message ---
--- Begin Message ---
A bit of an example to shed a little light?
function showstring($s='')
{
printf("String of length %d is %s\n",strlen($s),$s);
for ($i=0; $i < strlen($s); $i++) {
printf("s[%d] : '%s' %d ",$i,$s[$i],ord($s[$i]));
}
echo PHP_EOL;
}
$s = 'Now we are gone down to the river!';
showstring($s);
$s1 = $s;
$s1[strlen($s1)-1] = '';
showstring($s1);
$s2 = $s;
$s2 = substr($s2,0,-1);
showstring($s2);
Outputs:
String of length 34 is Now we are gone 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] : ' ' 32 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
String of length 34 is Now we are gone 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] : ' ' 32 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] : '' 0
String of length 33 is Now we are gone 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] : ' ' 32 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
---
Notice how in example 2, where the last character is replaced by a
null character string (''), the actual string length (and subsequently
string "array", if you will) is not shortened. However, in the third
example, it is shortened by substr making a new string.
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.
Perhaps of note, in C, a string is terminated by a null byte (i.e. ===
0) which could be why it works in webkit and gecko?
--- End Message ---