[PHP] Re: Strange string stuff -- maybe everything is ending...

2012-12-22 Thread Tim Streater
On 22 Dec 2012 at 16:50, Tedd Sperling t...@sperling.com wrote: 

 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.

That is explicitly documented here:

http://php.net/manual/en/language.types.string.php

String access and modification by character

Characters within strings may be accessed and modified by specifying the 
zero-based offset of the desired character after the string using square array 
brackets, as in $str[42]. Think of a string as an array of characters for this 
purpose. The functions substr() and substr_replace() can be used when you want 
to extract or replace more than 1 character.

--
Cheers  --  Tim

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[PHP] Re: Strange string stuff -- maybe everything is ending...

2012-12-21 Thread Jim Giner

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?


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php