Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert accents to HTML, but ignore tags

Tags:

html

php

encoding

The code below converts text for characters with accents. But it also converts the HTML tags which I would like to leave intact. How can I only convert accented characters and leave all other special characters intact? Thanks.

$temp = file_get_contents("file.html");
echo htmlentities($temp,ENT_NOQUOTES,'UTF-8');
like image 707
usertest Avatar asked Jan 23 '11 19:01

usertest


People also ask

What does htmlspecialchars() do?

The htmlspecialchars() function converts some predefined characters to HTML entities.

When to use htmlspecialchars?

The first question is: When to use the htmlspecialchars function? You use htmlspecialchars EVERY time you output content within HTML, so it is interpreted as content and not HTML. If you allow content to be treated as HTML, you have just opened the door to bugs at a minimum, and total XSS hacks at worst.

What is ENT_ compat?

ENT_COMPAT - Default. Encodes only double quotes. ENT_QUOTES - Encodes double and single quotes. ENT_NOQUOTES - Does not encode any quotes.


1 Answers

htmlspecialchars() and htmlspecialchars_decode() will only encode/decode &, <, >, ' and "; you could thus use the latter to convert their entities back to their HTML special characters:

echo htmlspecialchars_decode(htmlentities($temp, ENT_NOQUOTES, 'UTF-8'), ENT_NOQUOTES);
like image 149
BoltClock Avatar answered Sep 20 '22 16:09

BoltClock