PHP htmlentities not working even with parameters - php

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

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 :
<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
echo htmlentities(htmlentities($str));
gives:
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.

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.)

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. :(

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);

Related

htmlspecialchars in php not decoding

I have a application where I store sting as it is but while dispying, I want special characters to be converted to their HTML name like for < will be &lt. To achieve it, I am using php inbuilt function htmlspecialchars.
Output of text with this function is achieved with following code
$reviewTxt = htmlspecialchars($reviewTxt);
echo $reviewTxt;
Now, for reviewTxt to be 'I loved you <3', it should produce I loved you <3 but should display the original text. In my case, it displays the encoded data I loved you <3. I also tried to paste I loved you <3 instead of above php code just to see if I can get original text and yes, it shows 'I loved you <3'.
I am not sure what I am missing,
It looks like you are encoding twice with htmlspecialchars() / htmlentities().
That causes the & symbol of the first result to be encoded in the second result, giving you a string like I loved you &lt;3.
So it will show the encoded & followed by the litteral string lt;.

Repopulating textarea with EXACTLY what the user submitted (with ASCII codes and without additonal forward slashes before some characters)

I have a textarea, which I need to be able to take characters including / and ' as well as special characters in ASCII. It does this fine, and sends the data to a php page by the POST method.
Then I repopulate the text area simply by putting
<?php echo isset($F_Text) ? $F_Text : '' ?>
between the textarea tags ($F_Name = $_POST["F_Name"]), with the intention that the user can then alter what they typed in and resubmit.
But each time the form is repopulated two issues arise. A forward slash is added before characters such as ' and the ASCII characters are printed out as the symbol rather than the code. This basically breaks the rest of the page (the submission goes on to be processed by some javascript).
I can't think of any way to keep the ASCII codes as just that, codes, not symbols.
Also, I've just noticed that all $ signs are lost too, which I can understand, but I need them to stay!
I need the form to display EXACTLY what the user typed in originally. Any ideas?
Can you try with :
<?php echo isset($F_Text) ? htmlentities(stripslashes($F_Text)) : '' ?>
Hope this helps you :)
My guess would be that you first have to turn of magic quotes, then use htmlspecialchars to avoid that your variable messes up your html and then make sure everything is in utf8 so that all special characters are retained (depending on what you consider ASCII characters...).
Your php echo statement would be:
<?php echo isset($F_Text) ? htmlspecialchars($F_Text) : '' ?>

html entities for utf-8 character in php

I have used html_entities for UTF-8 in php.
$input = "<div> 'Testing' </div>";
echo htmlentities($input,ENT_NOQUOTES,"UTF-8");
But, above encoding is working for normal input, if i give below input and use encoding then I am getting blank output.
$input = "<div>Other 'user' is working on this line. Please contribute the next line.</div>";
echo htmlentities($input,ENT_NOQUOTES,"UTF-8");
I dont know how this is giving blank output.
If i print $input then I am getting below value in $input.
<div>Other user working on this line.�Please contribute the next line.</div>
Is any thing missed in htmlentities code, Please folks provide your suggestions.
Thanks,
-Pravin.
Try passing $input to utf8_encode first, and then passing the data to htmlentities with only the ENT_NOQUOTES option set:
<?php
$input = "<div>Other 'user' is working on this line. Please contribute the next line.</div>";
echo htmlentities(utf8_encode($input),ENT_NOQUOTES);
?>

PHP Ampersand in String

I'm having a bit of a problem. I am trying to create an IRC bot, which has an ampersand in its password. However, I'm having trouble putting the ampersand in a string. For example...
<?php
$var = "g&abc123";
echo $var;
?>
I believe this should print g&abc123. However it's printing g.
I have tried this as well:
<?php
$arr = array("key" => "g&abc123");
print_r($arr);
?>
This prints it correctly with the g&abc123, however when I say echo $arr['key']; it prints g again. Any help would be appreciated. I'm running PHP5.3.1.
EDIT: Also, I just noticed that if I use g&abc123&abc123 it prints g&abc123. Any suggestions?
I don't have that issue in a console:
php > $d="g&abc123";
php > echo $d;
g&abc123
What environment are you printing the output to? It sounds like you are viewing it in a web browser, and the & is being interpreted as a malformed HTML entity. Try replacing the & symbol with the entity encoded version &.
Look at the source code, it will be printing the correct code.
If you want it to print out correctly in HTML, then run htmlentities on it or make the & &
View the web page source to make sure your variable contains the correct value.
You're probably sending your output to a Web browser.
The correct way of doing it is
In HTML, XHTML and XML, the ampersand has a special meaning. It is used for character entities. You can think of it as an escape sequence of sorts.
For instance, in PHP, this would be illegal:
$variable = 'It's Friday';
This is because the apostrophe is interpreted by PHP as the end of your string, and the rest of your content looks like garbage.
Instead, you have to say:
$variable = 'It\'s Friday';
Similarly, in HTML and XHTML, you can't say
<h1>Inequalities</h1>
<p> x<yz+3 </p>
This is because it would be interpreted as an element.
Instead, you'd have to say:
<h1>Inequalities</h1>
<p> x<yz+3 </p>
Now, as you can see, the ampersand itself has a special meaning and, therefore, needs to be escaped as &. htmlspecialchars() will do it for you.

PHP str_replace/preg_replace problem with php open tags

I'm trying to replace something like:
$text = "Hello <--name--> !!";
echo str_replace("--","?",$text);
Expected:
Hello <?name?> !!
Result:
Hello !!
(I'm checking the source code, and I have short open tags enabled)
I have tried so many ways but it seems that I can't never have as result any <? (or <?php) string.
I think it may be related to Suhosin patch that is enabled by default in Ubuntu. Before doing anything else, does someone knows how to get that to work?
Thank you.
UPDATE:
I tried directly in command line and it worked. Yea, the problem was that anything between php tags is not displayed in the browser (Chrome), not even in the source code.
echo "A <"."?"."php"." echo 1 "." ?".">"." B";
In Chrome displays "A B" when looking at the source code. But Firefox displays it complete... So in summary Chrome was tricking me ;)
Thank you!!!
Sorry I had to choose the best answer... but for me the 3 answer were correct.
Did you really look into the source view of the browser? <? ?> sections tend to be interpreted as tags.
If you're not using eval() anywhere, there is no way these tags will be actually interpreted by PHP.
Maybe Suhosin filters those out but that would surprise me. You may be able to get around it by using
< >
instead.
It's got nothing to do with Suhosin.
<?name?> !! when displayed in an HTML page results in !!
Check the page source.
I agree with Pekka and Mike (the other Mike, not me Mike) - you really need to check the HTML source code, as it will show correctly. If you really want to see the less-than and greater-than symbols in the output, you need to replace those with HTML entities (as suggested by Pekka):
$search = array('<', '>', '--');
$replace = array('<', '>', '?');
$text = 'Hello <--name--> !!';
echo str_replace($search,$replace,$text);
You could also use htmlspecialchars, like this:
$text = htmlspecialchars("Hello <--name--> !!");
echo str_replace("--","?",$text); // Hello <?name?> !!
htmlspecialchars will replace:
& with &
" with "
< with <
with >
If you don't want to replace " for some reason or another it's possible (see http://se2.php.net/manual/en/function.htmlspecialchars.php). &, < and >, though, is as far as I know always replaced with &, < and > when you use htmlspecialchars.

Categories