Discussion:
[PHP-INSTALL] strtr array replace issue...
Brad Freshour
2009-02-09 20:19:59 UTC
Permalink
Apache: 2.2.8 threaded
PHP: 5.2.4-2
mySQL: 5.051.a

Ubuntu 8.04 minimal with everything needed installed via apt-get.

Using SMF forum, I'm having an issue with the strtr command... Here's
the code...

$ftp_file = strtr($filename, array ($_SESSION['pack_ftp']['root'] =>''));

The $_SESSION['pack_ftp']['root'] variable is an empty set: ''

Well, if the above strtr command was translated it would look like this:

strtr("/home/user/public_html/Packages/temp", array ('' =>''));

You would think that the return would be : /home/user/public_html/Packages/temp

However, it returns '' instead.

I can get around this by modifying the code to the site manually and
checking to see if $_SESSION['pack_ftp']['root'] == '' and if it does
assign it something crazy, like $_SESSION['pack_ftp']['root'] =
'%%%%%'

However, the code works on another site I have that's hosted for me
(shared hosting).

Anyone have any ideas?

I was wondering if it was related to this: http://bugs.php.net/bug.php?id=27457
But that seems like an old bug...

Thanks.
Keith Roberts
2009-02-09 22:46:27 UTC
Permalink
Subject: [PHP-INSTALL] strtr array replace issue...
Apache: 2.2.8 threaded
PHP: 5.2.4-2
mySQL: 5.051.a
Ubuntu 8.04 minimal with everything needed installed via apt-get.
Using SMF forum, I'm having an issue with the strtr command... Here's
the code...
$ftp_file = strtr($filename, array ($_SESSION['pack_ftp']['root'] =>''));
The $_SESSION['pack_ftp']['root'] variable is an empty set: ''
strtr("/home/user/public_html/Packages/temp", array ('' =>''));
You would think that the return would be : /home/user/public_html/Packages/temp
However, it returns '' instead.
I can get around this by modifying the code to the site manually and
checking to see if $_SESSION['pack_ftp']['root'] == '' and if it does
assign it something crazy, like $_SESSION['pack_ftp']['root'] =
'%%%%%'
However, the code works on another site I have that's hosted for me
(shared hosting).
Anyone have any ideas?
I was wondering if it was related to this: http://bugs.php.net/bug.php?id=27457
But that seems like an old bug...
Thanks.
You can run php as a 'BASIC-style' command-line interpreter
and debug awkward code like this one line at a time.

Just do:

[***@localhost keith]# php -a
Interactive shell

php > $a= "hello";
php > echo $a;
hello
php >

All the php functions are available in interactive mode.
That way you get immediate feedback on how your code works :)

Kind Regards,

Keith

-----------------------------------------------------------------
Websites:
http://www.php-debuggers.net
http://www.karsites.net
http://www.raised-from-the-dead.org.uk

All email addresses are challenge-response protected with
TMDA [http://tmda.net]
-----------------------------------------------------------------
Brad Freshour
2009-02-10 01:23:18 UTC
Permalink
Post by Brad Freshour
Apache: 2.2.8 threaded
PHP: 5.2.4-2
mySQL: 5.051.a
Ubuntu 8.04 minimal with everything needed installed via apt-get.
Using SMF forum, I'm having an issue with the strtr command... Here's
the code...
$ftp_file = strtr($filename, array ($_SESSION['pack_ftp']['root'] =>''));
The $_SESSION['pack_ftp']['root'] variable is an empty set: ''
strtr("/home/user/public_html/Packages/temp", array ('' =>''));
You would think that the return would be : /home/user/public_html/Packages/temp
However, it returns '' instead.
I can get around this by modifying the code to the site manually and
checking to see if $_SESSION['pack_ftp']['root'] == '' and if it does
assign it something crazy, like $_SESSION['pack_ftp']['root'] =
'%%%%%'
However, the code works on another site I have that's hosted for me
(shared hosting).
Anyone have any ideas?
I was wondering if it was related to this: http://bugs.php.net/bug.php?id=27457
But that seems like an old bug...
Thanks.
You can run php as a 'BASIC-style' command-line interpreter
and debug awkward code like this one line at a time.
Interactive shell
php > $a= "hello";
php > echo $a;
hello
php >
All the php functions are available in interactive mode.
That way you get immediate feedback on how your code works :)
Keith, thanks... no luck doing it that way either... Can someone else
try the snippet and see what is returned?

php > $a = "/home/user/public_html/Packages/temp";
php > $b = array('' => '');
php > echo strtr($a, $b);
php > echo $a;
/home/user/public_html/Packages/temp
php > print_r($b);
Array
(
[] =>
)
Christopher Jones
2009-02-10 01:54:25 UTC
Permalink
Post by Brad Freshour
Keith, thanks... no luck doing it that way either... Can someone else
try the snippet and see what is returned?
php > $a = "/home/user/public_html/Packages/temp";
php > $b = array('' => '');
php > echo strtr($a, $b);
php > echo $a;
/home/user/public_html/Packages/temp
php > print_r($b);
Array
(
[] =>
)
PHP 4.3.9, 5.2 and 5.3 all do the same. Note the return value from
phptr() is false (try var_dump instead of print_r)

You can debug what PHP is doing by breaking on php_strtr_array() in
ext/standard/standard.c. If a zero length key is seen, it returns
false.

As to expected behavior: feel free to log a bug, but be prepared to
defend your reasoning.

Chris
--
Email: ***@oracle.com Tel: +1 650 506 8630
Twitter: http://twitter.com/ghrd Free PHP Book: http://tinyurl.com/UGPOM
Loading...