Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can get the list of correspondances character => entity used by <a href="http://www.php.net/htmlentities" rel="noreferrer"><code>htmlentities</code></a>, with the function <a href="http://php.net/manual/en/function.get-html-translation-table.php" rel="noreferrer"><code>get_html_translation_table</code></a> ; consider this code :</p> <pre><code>$list = get_html_translation_table(HTML_ENTITIES); var_dump($list); </code></pre> <p><em>(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)</em></p> <p>It will get you something like this :</p> <pre><code>array ' ' =&gt; string '&amp;nbsp;' (length=6) '¡' =&gt; string '&amp;iexcl;' (length=7) '¢' =&gt; string '&amp;cent;' (length=6) '£' =&gt; string '&amp;pound;' (length=7) '¤' =&gt; string '&amp;curren;' (length=8) .... .... .... 'ÿ' =&gt; string '&amp;yuml;' (length=6) '"' =&gt; string '&amp;quot;' (length=6) '&lt;' =&gt; string '&amp;lt;' (length=4) '&gt;' =&gt; string '&amp;gt;' (length=4) '&amp;' =&gt; string '&amp;amp;' (length=5) </code></pre> <p>Now, remove the correspondances you don't want :</p> <pre><code>unset($list['"']); unset($list['&lt;']); unset($list['&gt;']); unset($list['&amp;']); </code></pre> <p>Your list, now, has all the correspondances character => entity used by htmlentites, except the few characters you don't want to encode.</p> <p>And now, you just have to extract the list of keys and values :</p> <pre><code>$search = array_keys($list); $values = array_values($list); </code></pre> <p>And, finally, you can use str_replace to do the replacement :</p> <pre><code>$str_in = '&lt;p&gt;&lt;font style="color:#FF0000"&gt;Camión español&lt;/font&gt;&lt;/p&gt;'; $str_out = str_replace($search, $values, $str_in); var_dump($str_out); </code></pre> <p>And you get :</p> <pre><code>string '&lt;p&gt;&lt;font style="color:#FF0000"&gt;Cami&amp;Atilde;&amp;sup3;n espa&amp;Atilde;&amp;plusmn;ol&lt;/font&gt;&lt;/p&gt;' (length=84) </code></pre> <p>Which looks like what you wanted ;-)</p> <p><br><em>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)</em></p> <p>Second edit couple of minutes after : it seem you'll have to use <code>utf8_encode</code> on the <code>$search</code> list, before calling <code>str_replace</code> :-(</p> <p>Which means using something like this :</p> <pre><code>$search = array_map('utf8_encode', $search); </code></pre> <p>Between the call to <code>array_keys</code> and the call to <code>str_replace</code>.</p> <p>And, this time, you should really get what you wanted :</p> <pre><code>string '&lt;p&gt;&lt;font style="color:#FF0000"&gt;Cami&amp;oacute;n espa&amp;ntilde;ol&lt;/font&gt;&lt;/p&gt;' (length=70) </code></pre> <p><br> And here is the full portion of code :</p> <pre><code>$list = get_html_translation_table(HTML_ENTITIES); unset($list['"']); unset($list['&lt;']); unset($list['&gt;']); unset($list['&amp;']); $search = array_keys($list); $values = array_values($list); $search = array_map('utf8_encode', $search); $str_in = '&lt;p&gt;&lt;font style="color:#FF0000"&gt;Camión español&lt;/font&gt;&lt;/p&gt;'; $str_out = str_replace($search, $values, $str_in); var_dump($str_in, $str_out); </code></pre> <p>And the full output :</p> <pre><code>string '&lt;p&gt;&lt;font style="color:#FF0000"&gt;Camión español&lt;/font&gt;&lt;/p&gt;' (length=58) string '&lt;p&gt;&lt;font style="color:#FF0000"&gt;Cami&amp;oacute;n espa&amp;ntilde;ol&lt;/font&gt;&lt;/p&gt;' (length=70) </code></pre> <p>This time, it should be ok ^^ <br> 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.</p> <p>Have fun !</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload