Re: Attempt at putting greedy htmlspecialchars on a diet
| From: | CPT John W. Holmes | Date: | Wed, 01 Oct 2003 11:55:11 +0000 |
| Subject: | Re: Attempt at putting greedy htmlspecialchars on a diet | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-164537@lists.php.net to get a copy of this message | ||
From: "Eugene Lee" <list-php-1@fsck.net>
> On Wed, Oct 01, 2003 at 01:12:16AM -0400, Gerard Samuel wrote:
> :
> : Got a problem with htmlspecialchars being too greedy, where
> : for example, it converts
> : &foo;
> : to
> : &foo;
> :
> : Yes it displays correctly in the browser for some content, but not all.
> : (an example is posted below)
> : So I came up with this example code, but not sure if there is an
> : easier/better way to get the correct end result.
> : If there is a better way, feel free to let me know.
> : Thanks
> :
> : Note: I dont read/speak chinese, so if its offensive please forgive me.
> :
> : ------
> : <?php
> :
> : $foo = '中文 &
> http://www.foo.com/index.php?foo=1&bar=2';
>
> The problem isn't with htmlspecialchars(). It doesn't know what parts
> of the string are HTML character references and which parts are not.
> But if you're willing to dig up the numeric character references for
> those specific Chinese characters, then split the string into the part
> that needs no translation and the part that needs it. That is:
>
> $foo1encoded = '中文'
> $foo2raw = ' &
> http://www.foo.com/index.php?foo=1&bar=2';
> $foo = $foo1 . htmlspecialchars(foo2raw);
Maybe you should run html_entity_decode() on the string first, then run
encode again. The decode will take 中 and turn it into it's actual
character but not affect anything else. Then the recoding will turn it back
into 中 and also encode any other characters.
---John Holmes...