Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I preg_replace special character like 'Prêt-à-porter'?
    text
    copied!<p>There are heaps of Qs about this on this forum and on the web in general. But I don't just get it.</p> <p>Here is my code:</p> <pre><code>function updateGuideKeywords($dal) { $pattern = "/[^a-zA-Z-êàé]/"; $keywords = preg_replace($pattern, '', $_POST['keywords']); echo json_encode($keywords); } </code></pre> <p>Now, the input is <code>Prêt-à-porter</code>, and the output is <code>"Pr\u00eat-\u00e0-porter"</code>.</p> <p>Why do I get the '\u00e' ?</p> <p>And how can I alter my pattern to include the characters <code>ê</code>, <code>à</code> and <code>é</code> ?</p> <p><strong>EDIT</strong><br> humm... since it looks like a unicode / character issue, I might go for the solution I found on <a href="http://www.codeguru.com/forum/archive/index.php/t-318709.html" rel="nofollow noreferrer">this page</a>.</p> <p>Here they suggest doing something like this:</p> <pre><code>$chain="prêt-à-porter"; $pattern = array("'é'", "'è'", "'ë'", "'ê'", "'É'", "'È'", "'Ë'", "'Ê'", "'á'", "'à'", "'ä'", "'â'", "'å'", "'Á'", "'À'", "'Ä'", "'Â'", "'Å'", "'ó'", "'ò'", "'ö'", "'ô'", "'Ó'", "'Ò'", "'Ö'", "'Ô'", "'í'", "'ì'", "'ï'", "'î'", "'Í'", "'Ì'", "'Ï'", "'Î'", "'ú'", "'ù'", "'ü'", "'û'", "'Ú'", "'Ù'", "'Ü'", "'Û'", "'ý'", "'ÿ'", "'Ý'", "'ø'", "'Ø'", "'œ'", "'Œ'", "'Æ'", "'ç'", "'Ç'"); $replace = array('e', 'e', 'e', 'e', 'E', 'E', 'E', 'E', 'a', 'a', 'a', 'a', 'a', 'A', 'A', 'A', 'A', 'A', 'o', 'o', 'o', 'o', 'O', 'O', 'O', 'O', 'i', 'i', 'i', 'I', 'I', 'I', 'I', 'I', 'u', 'u', 'u', 'u', 'U', 'U', 'U', 'U', 'y', 'y', 'Y', 'o', 'O', 'a', 'A', 'A', 'c', 'C'); $chain = preg_replace($pattern, $replace, $chain); </code></pre> <p><strong>EDIT 2</strong><br> This is my solution so far:</p> <pre><code>function updateGuideKeywords() { //First we replace characters with accents $pattern = array("'é'", "'è'", "'ë'", "'ê'", "'É'", "'È'", "'Ë'", "'Ê'", "'á'", "'à'", "'ä'", "'â'", "'å'", "'Á'", "'À'", "'Ä'", "'Â'", "'Å'", "'ó'", "'ò'", "'ö'", "'ô'", "'Ó'", "'Ò'", "'Ö'", "'Ô'", "'í'", "'ì'", "'ï'", "'î'", "'Í'", "'Ì'", "'Ï'", "'Î'", "'ú'", "'ù'", "'ü'", "'û'", "'Ú'", "'Ù'", "'Ü'", "'Û'", "'ý'", "'ÿ'", "'Ý'", "'ø'", "'Ø'", "'œ'", "'Œ'", "'Æ'", "'ç'", "'Ç'"); $replace = array('e', 'e', 'e', 'e', 'E', 'E', 'E', 'E', 'a', 'a', 'a', 'a', 'a', 'A', 'A', 'A', 'A', 'A', 'o', 'o', 'o', 'o', 'O', 'O', 'O', 'O', 'i', 'i', 'i', 'I', 'I', 'I', 'I', 'I', 'u', 'u', 'u', 'u', 'U', 'U', 'U', 'U', 'y', 'y', 'Y', 'o', 'O', 'a', 'A', 'A', 'c', 'C'); $shguideID = $_POST['shguideID']; $keywords = preg_replace($pattern, $replace, $_POST['keywords']); //Then we remove unwanted characters by only allowing a-z, A-Z, comma, 'minus' and white space $keywords = preg_replace("/[^a-zA-Z-,\s]/", "", $keywords); echo json_encode($keywords); } </code></pre>
 

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