Posted in php functions
6732
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
105
This Month
493
This Year
1738

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
“If you just sit and observe, you will see how restless your mind is. If you try to calm it, it only makes it worse, but over time it does calm, and when it does, there’s room to hear more subtle things. You see so much more than you could see before. It’s a discipline; you have to practice it.”
Steve Jobs
Random CSS Property

inherit

The inherit CSS keyword causes the element for which it is specified to take the computed value of the property from its parent element. It can be applied to any CSS property, including the CSS shorthand all.
inherit css reference