Str: preg_replace (): No ending delimiter '-' found

0

Hello could you help me where exactly to place the delimiters in the code below?

    function simplifica($txt){
    $txt = strtolower($txt);
    $txt = str_replace(array('á','à','â','ã','å','ä','ª','Á','À','Â','Ã','Ä'), 'a', $txt);
    $txt = str_replace(array('é','è','ê','ë','É','È','Ê','Ë'), 'e', $txt);
    $txt = str_replace(array('í','ì','î','ï','Í','Ì','Î','Ï'), 'i', $txt);
    $txt = str_replace(array('ó','ò','ô','õ','ö','º','Ó','Ò','Ô','Õ','Ö'), 'o', $txt);
    $txt = str_replace(array('ú','ù','û','ü','Ú','Ù','Û','Ü'), 'u', $txt);
    $txt = str_replace(array('ñ','Ñ'), 'n', $txt);
    $txt = str_replace(array('ç','Ç'), 'c', $txt);
    /*
    $txt = ereg_replace('[áàâãåäªÁÀÂÄÃ]', 'a', $txt);
    $txt = ereg_replace('[íìîïÍÌÎÏ]', 'i', $txt);
    $txt = ereg_replace('[éèêëÉÈÊË]', 'e', $txt);
    $txt = ereg_replace('[óòôõöºÓÒÔÕÖ]', 'o', $txt);
    $txt = ereg_replace('[úùûüÚÙÛÜ]', 'u', $txt);
    $txt = ereg_replace('[ñÑ]', 'n', $txt);
    $txt = ereg_replace('[çÇ]', 'c', $txt);
    */
    $txt = preg_replace("\n|\t|\r", '', $txt);
    $txt = preg_replace('[ ]+', ' ', $txt); // de 2 espaços p/ nada
    $txt = preg_replace('[ ]', '-', $txt); // de 2 espaços p/ nada (denovo)
    $txt = preg_replace('-+', '-', $txt); // de 2 espaços p/ nada (denovo)
    return preg_replace('/([^a-z0-9-]*)/', '', $txt);
    
asked by anonymous 16.03.2015 / 19:32

2 answers

0

You forgot to put the delimiters again in preg_replace .

function simplifica($txt){
    $txt = strtolower($txt);
    $txt = str_replace(array('á','à','â','ã','å','ä','ª','Á','À','Â','Ã','Ä'), 'a', $txt);
    $txt = str_replace(array('é','è','ê','ë','É','È','Ê','Ë'), 'e', $txt);
    $txt = str_replace(array('í','ì','î','ï','Í','Ì','Î','Ï'), 'i', $txt);
    $txt = str_replace(array('ó','ò','ô','õ','ö','º','Ó','Ò','Ô','Õ','Ö'), 'o', $txt);
    $txt = str_replace(array('ú','ù','û','ü','Ú','Ù','Û','Ü'), 'u', $txt);
    $txt = str_replace(array('ñ','Ñ'), 'n', $txt);
    $txt = str_replace(array('ç','Ç'), 'c', $txt);

    $txt = preg_replace("/\n|\t|\r/", '', $txt);
    $txt = preg_replace("/[ ]+/", ' ', $txt); // de 2 espaços p/ nada
    $txt = preg_replace("/[ ]/", '-', $txt); // de 2 espaços p/ nada (denovo)
    $txt = preg_replace("/-+/", '-', $txt); // de 2 espaços p/ nada (denovo)
    return preg_replace('/([^a-z0-9-]*)/', '', $txt);
}
    
16.03.2015 / 20:26
0

I found the function below in another forum and I think it answers this question well:

function tirarAcentos($string){<br>
    $string2 = preg_replace(array("/(á|à|ã|â|ä)/","/(Á|À|Ã|Â|Ä)/","/(é|è|ê|ë)/","/(É|È|Ê|Ë)/","/(í|ì|î|ï)/","/(Í|Ì|Î|Ï)/","/(ó|ò|õ|ô|ö)/","/(Ó|Ò|Õ|Ô|Ö)/","/(ú|ù|û|ü)/","/(Ú|Ù|Û|Ü)/","/(ñ)/","/(Ñ)/", "/(ç|Ç)/"),explode(" ","a A e E i I o O u U n N c"),$string);<br>
    return $string2;<br>
}
    
04.03.2017 / 05:28