Posted in php functions
5744
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
86
This Month
573
This Year
750

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
For a long time it had seemed to me that life was about to begin - real life. But there was always some obstacle in the way, something to be gotten through first, some unfinished business, time still to be served, or a debt to be paid. Then life would begin. At last it dawned on me that these obstacles were my life.
Alfred D. Souza
Random CSS Property

place-self

The place-self CSS shorthand property allows you to align an individual item in both the block and inline directions at once (i.e. the align-self and justify-self properties) in a relevant layout system such as Grid or Flexbox. If the second value is not present, the first value is also used for it.
place-self css reference