Stomped
Stomped

Reputation: 2100

Not encoding already encoded items with htmlentities

I have a string that looks something like this:

Bürstner    

When I use htmlentities() on it, I set the double encode param to false, but it still ends up re-encoding the   into  

I'm using this to encode:

$out = htmlentities($string,ENT_NOQUOTES, 0);

Am I somehow misunderstanding how this works? The desired output is to encode the umlaut u, but leave the existing nbsp entities alone (this is just an example, there are MANY entities in a very long document already).

** EDIT **

Since this seems unclear, ORIGINAL STRING:

Bürstner   

DESIRED OUTPUT:

Bürstner   

The existing entities should be left alone.

Upvotes: 2

Views: 531

Answers (5)

streetparade
streetparade

Reputation: 32888

Have a look at this function

The link for it http://php.net/manual/de/function.htmlspecialchars.php

<?php
function special_formatting($input) {
    $output = htmlspecialchars($input, ENT_QUOTES);
    $output = str_replace(array('  ', "\n"), array('&nbsp;&nbsp;', '<br>'), $output);
    return str_replace('&nbsp; ', '&nbsp;&nbsp;', $output);
}
?>

Upvotes: 0

Pekka
Pekka

Reputation: 449415

It seems to me that you overlooked the third parameter to htmlentities():

string htmlentities ( string $string [, int $quote_style = ENT_COMPAT [, string $charset [, bool $double_encode = true ]]] )

try

$out = htmlentities($string, ENT_NOQUOTES, <whatever encoding you're using>, false);

Upvotes: 2

harpax
harpax

Reputation: 6106

The 3rd parameter of htmlentities is the charset.. you would need to set the 4th to false

string htmlentities ( string $string [, int $quote_style = ENT_COMPAT [, string $charset [, bool $double_encode = true ]]] )

http://www.php.net/manual/en/function.htmlentities.php

Upvotes: 2

Thomas
Thomas

Reputation: 181745

The third argument is the charset; you need to set the fourth, not the third, to false.

Upvotes: 4

Gumbo
Gumbo

Reputation: 655239

The third parameter of htmlentities is the charset parameter; the fourth parameter is the double_encode parameter. So try this:

$out = htmlentities($string, ENT_NOQUOTES, ini_get('default_charset'), false);

Upvotes: 11

Related Questions