View Full Version : Replace special chars not tags, php trigered by html

09-20-2009, 04:33 PM
Good day to you all,
I working on a code which take a txt file and display it through php, but this thing is triggered by ajax.

in the txt files there is html tags and html special chars.

What I'm trying to do is encode only the special chars.
ex :

Php read those character not a problem, it when I pass it by ajax that i get some weird chars like squares with a question mark.

Does anybody have an idea on how I can fix this ?

Here is the code that get triggered when i click on a ajax link :

$textsectb = file_get_contents('dep_en_tb.txt');
$textsechttb = htmlentities($textsectb);
echo $textsechttb;

I only need to replace the special char not the html tags.


09-20-2009, 06:58 PM
In which encoding 'dep_en_tb.txt' is saved ? If it is e.g. utf-8, I think you could try the following:

$textsectb = file_get_contents('dep_en_tb.txt');
header('Content-type: text/plain; charset=utf-8');
echo $textsechttb;

This code would not encode anything at all. Still if the document is transferred with the proper encoding, I think you should not get squares or question marks instead of the entities...

Of course I supposed that you have been working with responseText (not responseXML) property of XMLHttpRequest object.

You said you would not like to encode the HTML. Please notice, that in some tasks not encoding HTML could create certain security problems.

Edit: if you really would like to actually encode the entities, you could do it like (please remove spaces from strings like '& #233;' - the system did not allow me to post otherwise converting entities to characters):

$textsectb = str_replace(array('','',''),array('& #233;','& #232;','& #236;'),$textsectb); // please remove spaces from strings like '& #233;'!

Please see this table (http://www.w3schools.com/tags/ref_entities.asp) for reference how entities could be encoded ...

09-20-2009, 07:00 PM
I don't have an answer to this, but I wanted to
post this link, as the discussion is interesting, and
you might get some ideas about your problem:


You didn't mention if your special characters are based on foreign languages,
or are they embedded RTF codes, like tabs, indents, etc.