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
$TEST = "Kold Locker™ & other stuff"; echo "ORGINIAL: " . $TEST . "<BR/>"; echo "HTML: " . htmlentities($TEST, ENT_COMPAT, 'UTF-8');
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
Your code works for me 😕
In the manual page for
htmlentities() we can read:
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.)
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');
<html> <head><head>something like this </html>
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
A 'quote' is <b>bold</b>
(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 < and > as > and <.
(See the source code in the browser). Second call of htmlentities translates & into
& and only then the Browser shows what you expected in the first place.
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:
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,
EDIT: 3. If you are using a
$_POST formular, don´t forget to add
accept-charset="UTF-8" (or some other charset) to the
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. 🙁