Posted in php functions
5858
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
133
This Month
546
This Year
864

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
When I realized that, no individual step is hard in any process. Building this airport I'm standing in right now started with a guy writing the architectural plans on paper. That's not hard for him to do. Then laying the first beam isn't had. The whole thing is really hard. So, just take each step kind of piece by piece and when I was able to do that and stop trying to chase this prize and started putting in the work, things just started coming together.
Unknown
Random CSS Property

justify-items

The CSS justify-items property defines the default justify-self for all items of the box, giving them all a default way of justifying each box along the appropriate axis.
justify-items css reference