Gil Birman
Gil Birman

Reputation: 35900

PHP's htmlentities function doesn't encode ★ star

htmlentities($this->name, null, "UTF-8");

Does not encode the star (★). How can I make it encode the star?

Update: &acirc doesn't render a star. Also, I'm using:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Upvotes: 1

Views: 357

Answers (2)

dev-null-dweller
dev-null-dweller

Reputation: 29462

How can I make it encode the star?

You can't. htmlentities does not encode all Unicode characters. But you can try some workarounds, like this one

Or you can make use of output from json_encode:

$txt = preg_replace_callback(
    '/[\x80-\xFF]{3,}/', //do not trust this, it's only example that works for small range of unicode characters, that happens to include black star 
     function($m){ 
         return str_replace('\\u','&#x',trim(json_encode($m[0]),'"')).';';
      }, "Black ★ Star"
); // Black &#x2605; Star

Upvotes: 2

nickb
nickb

Reputation: 59699

In PHP >= 5.4, the default value of the encoding parameter was changed to UTF-8.

If you use:

htmlentities( "★", null, "ISO-8859-1");

The star character will be converted to &acirc;.

Upvotes: 2

Related Questions