Posted in php functions
6841
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
151
This Month
602
This Year
1847

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
Our brains are wired to find things we are looking for - if your always cynical or waiting for things to go wrong, then your life will reflect that. On the other hand, having a positive outlook on life will bring you joy and provide you with inspiration when you least expect it ☀️🍉🌴
Unknown
Random CSS Property

max-inline-size

The max-inline-size CSS property defines the horizontal or vertical maximum size of an element's block, depending on its writing mode. It corresponds to either the max-width or the max-height property, depending on the value of writing-mode.
max-inline-size css reference