Re: Strange string stuff -- maybe everything is ending...
| From: | tamouse mailing lists | Date: | Sat, 22 Dec 2012 12:58:00 +0000 |
| Subject: | Re: Strange string stuff -- maybe everything is ending... | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-319935@lists.php.net to get a copy of this 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?