Posted in php functions
6817
8:48 am, February 19, 2022
 

convert a string into its html entitles using php htmlentities function

this takes a html string such as the following example and converts it into its html entities.

it will take a character such as < and convert it into &lt; so that the string is not parsed as html and can be embedded into code or a view page. 

Update it seems that the browser is taking the &lt and the other characters and converting them into the html entities again when you inspect them in the browser. 

I wonder how to get the encoded or decoded string and add it to a textarea, im sure this used to work. 

PHP

$string = "<a href='#i_am_the_link_target'>Im a Link text</a>.";
$string_htmlentities = htmlentities($string);
echo "<h3>The original String</h3>";
echo $string;
echo "<h3>The String after being converted</h3>";
echo $string_htmlentities;
echo "<p>if you want to see the encoded characters you can always add it to a text areas.</p>";
echo "<textarea>$string_htmlentities</textarea>";
echo "<p>you can also decode the entities with the following function<p>";
$string_htmlentities_decode = html_entity_decode($string_htmlentities);
echo $string_htmlentities_decode;
echo "<textarea>$string_htmlentities_decode</textarea>";
echo htmlspecialchars($string);
  echo "<textarea>";
echo htmlspecialchars($string);
echo "</textarea>";
echo "<p>actually the one i was looking for here was htmlspecialchars_decode got there in the end!</p>";
echo htmlspecialchars_decode($string);
echo "<textarea>";
echo htmlspecialchars_decode($string);
echo "</textarea>";

echo htmlspecialchars($string, ENT_QUOTES, 'UTF-8');

View Statistics
This Week
127
This Month
578
This Year
1823

No Items Found.

Add Comment
Type in a Nick Name here
 
Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
Nothing, to my way of thinking, is a better proof of a well ordered mind than a man's ability to stop just where he is and pass some time in his own company.
Seneca
Random CSS Property

perspective()

The perspective() CSS function defines a transformation that sets the distance between the user and the z=0 plane, the perspective from which the viewer would be if the 2-dimensional interface were 3-dimensional. Its result is a <transform-function> data type.
perspective() css reference