Passing html entities and processing them.

Basically, this deals with having a form that passes an html entity. When PHP reads this, it's not in html entity form anymore. Below is two examples. The first one was the problem, and the second is the solution.

<?php
extract($_REQUEST);

echo($var);

$html = <<<eof
<html>
<head>
<title>test</title>
</head>
<body>
<form action="{$_SERVER['PHP_SELF']}" method="post">
<select name="var">
<option value="M&amp;M &#169; &copy;">M&amp;M &#169; &copy;</option>
</select> <input type="submit" value="go" />
</form>
</body>
</html>
eof
;

echo($html);
?>

If you execute the code above in a browser, you will see that the © symbol will be converted into the actuall copyright symbol itself and not an html entity. I was wanting to put these into a database and then compare that value to the one predefined in an array. The point is, it was comparing © to (C) where (C) was the actual copyright symbol.

Below, just add the function htmlentities() around the variable being passed and it will convert it back into the html entity. In my case, ©.

It's worth noting, that htmlentities() will convert the copyright symbol into © and not ©. I was actually using the character code instead of © and the comparison in the database didn't work out. htmlentities() put © in the database, and the array in my script was using © so they never matched.

<?php
extract($_REQUEST);

echo(htmlentities($var));

$html = <<<eof
<html>
<head>
<title>test</title>
</head>
<body>
<form action="{$_SERVER['PHP_SELF']}" method="post">
<select name="var">
<option value="M&amp;M &#169; &copy;">M&amp;M &#169; &copy;</option>
</select> <input type="submit" value="go" />
</form>
</body>
</html>
eof
;

echo($html);
?>

Page Comments (Click to edit)






[Click to add or edit comments])

Please prepend comments below including a date

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Have a nice day.