PHP htmlentities not working even with parameters
Asked Answered
U

5

9

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

Urita answered 4/4, 2013 at 15:49 Comment(10)
Set display_errors to 1 and set error_reporting to E_ALL. You're likely getting an error message.Scoop
I have checked error reporting with none listedUrita
PHP Version 5.3.3-1ubuntu9.10Urita
Well you definitely want htmlentities not htmlspecialchars.Ramadan
Could this have anything to do with HEADER information missing from code...even though I am putting in the encoding in the third parameter?Urita
Try htmlentities($TEST, ENT_COMPAT | ENT_IGNORE, "UTF-8");Ramadan
It displays but it removes the TM (Trademark symbol) and keeps the ampersand: HTML: Kold Locker & other stuffUrita
works with php 5.3.3-7+squeeze14Rodeo
Are you examining the generated HTML source or the text rendered in the document view of the browser?Maxey
Both. There is nothing for both.Urita
H
4

Your code works for me :-?

In the manual page for htmlentities() we can read:

Return Values

Returns the encoded string.

If the input string contains an invalid code unit sequence within the given encoding an empty string will be returned, unless either the ENT_IGNORE or ENT_SUBSTITUTE flags are set.

My guess is that the input data is not properly encoded as UTF-8 and the function is returning an empty string. (Assuming that the script is not crashing, i.e., code after that part still runs.)

Hideout answered 4/4, 2013 at 16:1 Comment(1)
I think you are on to something, let me try and encode before adding htmlentitiesUrita
E
8

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;

Epigraphic answered 4/6, 2013 at 0:49 Comment(1)
It is strange, because I have to basically call htmlentities twice to get it to encode it. Why? It weird because I've never had this problem before and out of nowhere it won't work by itself.Troopship
T
6

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.

Tricky answered 31/8, 2013 at 8:37 Comment(0)
H
4

Your code works for me :-?

In the manual page for htmlentities() we can read:

Return Values

Returns the encoded string.

If the input string contains an invalid code unit sequence within the given encoding an empty string will be returned, unless either the ENT_IGNORE or ENT_SUBSTITUTE flags are set.

My guess is that the input data is not properly encoded as UTF-8 and the function is returning an empty string. (Assuming that the script is not crashing, i.e., code after that part still runs.)

Hideout answered 4/4, 2013 at 16:1 Comment(1)
I think you are on to something, let me try and encode before adding htmlentitiesUrita
F
1

I had almost the same problem (in which somehow it showed the same text every time) and with a combination of different echo´s i got it. It seems that webbrowsers like firefox show the same text every time. That´s because when you echo the htmlentities-text, its being converted back into normal text while echoing. When I echo a script with the variable/text to be console.logged, it actually echo´s the htmlentities text (almost) correctly. Instead of replacing every special char with html-codings, it replaces ´em with some other coding i already saw before (I can´t remember the name). Htmlentities-ing it again, I get the same text echo´d again (remember it converts everything), but echoing it in console.log-version gives to me the expected result. Now, again, as a result:
1. Execute htmlentities two times!
2. Don´t (at least with firefox) echo the htmlentities as normal into the webpage. If you´d like to check if the value is actually correct, echo a script that logs it into console.
I hope this could help other guys with the same problem,
VicStudio

EDIT: 3. If you are using a $_POST formular, don´t forget to add accept-charset="UTF-8" (or some other charset) to the <form> tag.

EVEN MORE EDIT: Only do 2 times htmlentities if you wish to echo your result normal into the page. If you wish to directly send in f.e. a database, only do it once! -> what i said before is partually wrong. :(

Fluviomarine answered 9/7, 2017 at 10:2 Comment(0)
S
0

This is an old post, but for anyone still looking for a solution, here is what I use with success:

echo html_entity_decode($htmlString);
Sisyphus answered 2/9, 2020 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.