Query:
Code:
Link:
example a link: localhost / codeigneter / search / region-of-arica-and-parinacota / aysen
Instead of manually listing the "problematic" characters, what you can do is a regular expression to say: "all non-ASCII characters".
The ASCII characters are those between \x00
and \x7F
. ( This I learned in StackOverflow ) The printable ASCII characters are between \x20
and \x7F
, therefore to eliminate everything that does not fall in that range you do:
$jstring2 = preg_replace('/[^(\x20-\x7F)]*/', '', $jcountry);
From then on it is up to you what to do with the spaces and punctuation marks.
If what you are looking for is to generate a URL
without special characters, such as: /search/region-de-arica-y-parinacota/aysen
What you would be missing is to replace each caracter acentuado
with its corresponding without accent .
Example:
<?php
function urlCleanString($str) {
// Quitamos los caracteres "desconocidos"
$str = preg_replace('/[^a-zA-Z0-9ñÑáéíóúÁÉÍÓÚ ]/', '', $str);
// Reemplazamos los espacios por guiones (-)
$str = strtolower(preg_replace('/\s+/', '-', $str));
// Reemplazamos la caracteres acentuados
return str_replace(array('á','é','í','ó','ú','ñ'), array('a','e','i','o','u','n'), $str);
}
$jcountry = 'Región de arica y parinacota';
$jcity = 'Aysén';
echo 'search/'.urlCleanString($jcountry).'/'.urlCleanString($jcity);
//Output: search/region-de-arica-y-parinacota/aysen
PD : Your RegExp
to remove "unknown characters" was missing the space ( ) and that's why your
$paramN
had no hyphens ( -
)
It's actually quite simple:
We load the helper text in the controller, where you need it or directly in the autoload
// Cargamos el helper
$this->load->helper('text');
// Forma de Implementación
$str = 'áéíóú';
$str = convert_accented_characters($str);
echo $str; // aeiou