I want to convert all texts in a string into html entities but preserving the HTML tags, for example this:
<p><font style="color:#FF0000">Camión español</font></p>
should be translated into this:
<p><font style="color:#FF0000">Camión español</font></p>
any ideas?
You can get the list of correspondances character => entity used by
htmlentities, with the functionget_html_translation_table; consider this code :(You might want to check the second parameter to that function in the manual — maybe you’ll need to set it to a value different than the default one)
It will get you something like this :
Now, remove the correspondances you don’t want :
Your list, now, has all the correspondances character => entity used by htmlentites, except the few characters you don’t want to encode.
And now, you just have to extract the list of keys and values :
And, finally, you can use str_replace to do the replacement :
And you get :
Which looks like what you wanted 😉
Edit : well, except for the encoding problem (damn UTF-8, I suppose — I’m trying to find a solution for that, and will edit again)
Second edit couple of minutes after : it seem you’ll have to use
utf8_encodeon the$searchlist, before callingstr_replace🙁Which means using something like this :
Between the call to
array_keysand the call tostr_replace.And, this time, you should really get what you wanted :
And here is the full portion of code :
And the full output :
This time, it should be ok ^^
It doesn’t really fit in one line, is might not be the most optimized solution ; but it should work fine, and has the advantage of allowing you to add/remove any correspondance character => entity you need or not.
Have fun !