Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP htmlentities not working even with parameters

Of course this has been asked before and have searched for solutions, all which have not worked thus far. I want to change out the TM symbol and the ampersand to their html equivelents by using htmlentities or htmlspecialchars:

$TEST = "Kold Locker™ & other stuff";
echo "ORGINIAL: " . $TEST . "<BR/>";

echo "HTML: " . htmlentities($TEST, ENT_COMPAT, 'UTF-8');

This displays:

ORGINIAL: Kold Locker™ & other stuff
HTML: 

I have also tried it with htmlspecialchars and the second parameter changed with the same result.

What am I missing that others have claimed worked in other solutions?

UPDATE: I tried just displaying utf8_encode($TEST) and it displayed HTML: Kold Locker™ & other stuff

like image 981
ToddN Avatar asked Apr 04 '13 15:04

ToddN


People also ask

What's the difference between HTML entities () and htmlspecialchars ()?

Difference between htmlentities() and htmlspecialchars() function: The only difference between these function is that htmlspecialchars() function convert the special characters to HTML entities whereas htmlentities() function convert all applicable characters to HTML entities.

What is HTML entities () function?

The htmlentities() function converts characters to HTML entities. Tip: To convert HTML entities back to characters, use the html_entity_decode() function. Tip: Use the get_html_translation_table() function to return the translation table used by htmlentities().

What is the use of Htmlspecialchars in PHP?

The htmlspecialchars() function converts some predefined characters to HTML entities.

What will the function HTML entities transform a double quote character into?

Entity-quoting only HTML syntax characters The following entities are converted: Ampersands ( & ) are converted to &amp; Double quotes ( " ) are converted to &quot; Single quotes ( ' ) are converted to &#039; (if ENT_QUOTES is on, as described for htmlentities( ) )


2 Answers

I dont know why , this worked for me (htmlentities has to be called twice for me)

$html="<html> <head><head>something like this   </html>"
$entities_correction= htmlentities( $html, ENT_COMPAT, 'UTF-8');
echo  htmlentities( $entities_correction, ENT_COMPAT, 'UTF-8');

output :

&lt;html&gt; &lt;head&gt;&lt;head&gt;something like this &lt;/html&gt;

like image 59
internals-in Avatar answered Oct 21 '22 02:10

internals-in


I thought I had the same problem as Pjack (msg of Jul 14 at 8:54):

$str = "A 'quote' is <b>bold</b>";
echo htmlentities($str); 

gives in the Browser (Firefox in my case) the original string $str (without any translation), while

echo htmlentities(htmlentities($str));

gives:

A 'quote' is &lt;b&gt;bold&lt;/b&gt; 

(I use PHP/5.4.16 obtained from windows-7 XAMPP).

However, after some more thought it occurred to me that the Browser shows the strings &lt; and &gt; as > and <. (See the source code in the browser). Second call of htmlentities translates & into &amp; and only then the Browser shows what you expected in the first place.

like image 41
P. Wormer Avatar answered Oct 21 '22 03:10

P. Wormer