How to format title for url format?

4

For example, I have a title: 'Test Title Url'. But I need to leave it in url format, is there any function that does this?

    
asked by anonymous 15.04.2015 / 16:48

3 answers

7

There is yes, use strtolower and url_title .

strtolower : Returns string with all characters converted to lowercase.

url_title : Get a string as input and create a friendly URL string.

Example :

$title = 'Teste titulo Url';

$title_url = strtolower(url_title($title));

Returns: teste-titulo-url .

Ps: url_title is part of URL Helper of CodeIgniter, which should be loaded using:

$this->load->helper('url');
    
15.04.2015 / 16:50
4
  

There is yes, use strtolower and url_title .

     

strtolower : Returns string with all characters converted to lowercase.

     

url_title : Get a string as input and create a friendly URL string.

     

Example :

$title = 'Teste titulo Url';

$title_url = strtolower(url_title($title));
     

Returns: teste-titulo-url .

     

Ps: url_title is part of URL Helper of CodeIgniter, which should be   loaded using:

$this->load->helper('url');

@GWER forgot to mention that there are few accents in English so this function excludes letters with accents, create a function before using url_title() to replace the special characters of the url, for example:

//remove acentos e caracteres especiais de uma string
function remove_acentos($string = NULL){
    $procurar   = array('À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','Ø','Ù','Ú','Û','Ü','Ý','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ñ','ò','ó','ô','õ','ö','ø','ù','ú','û','ü','ý','ÿ');
    $substituir = array('A','A','A','A','A','A','AE','C','E','E','E','E','I','I','I','I','D','N','O','O','O','O','O','O','U','U','U','U','Y','s','a','a','a','a','a','a','ae','c','e','e','e','e','i','i','i','i','n','o','o','o','o','o','o','u','u','u','u','y','y');
    return str_replace($procurar, $substituir, $string);

}

//função que gera um slug com base no título
function slug($string = NULL){
    $string = remove_acentos($string);
    return url_title($string, '-', TRUE);//função do helper url | url_title(DA_ONDE_PEGA_OS_DADOS, O SEPARADOR ENTRE AS PALAVRAS, BOOLEAN TUDO MINUSCULO OU NÃO)
}
    
15.04.2015 / 16:57
-1
    public function url_maker($string = NULL){
    $procurar   = array('À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í',
        'Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','Ø','Ù','Ú','Û','Ü','Ý','ß','à','á',
        'â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ñ','ò','ó','ô',
        'õ','ö','ø','ù','ú','û','ü','ý','ÿ','}',']','°', '+', '(',')','*','#','@','!','#','$','%','¨',':','’','‘',',');
   $substituir = array('A','A','A','A','A','A','AE','C','E','E','E','E','I','I',
       'I','I','D','N','O','O','O','O','O','O','U','U','U','U','Y','s','a','a',
       'a','a','a','a','ae','c','e','e','e','e','i','i','i','i','n','o','o','o',
       'o','o','o','u','u','u','u','y','y',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','','','',' ');
    $replace = str_replace($procurar, $substituir, $string);
    $replace = str_replace(' ', '-', $replace);
    $replace = str_replace(array('-----', '----', '---', '--'), '-', $replace);
    return $replace;

}

$Name = "gabriel ariza ---gomes de castro ! teste } ] é usuário do SOpt";

        echo "<a href='".$this->url_maker($Name)."'>".$Name."</a>";
    
30.07.2015 / 19:01