How to format title to url format?

Asked

Viewed 1,170 times

4

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

3 answers

7


There is, use it strtolowerand url_title.

strtolower : Returns string with all characters converted to lowercase.

url_title : Takes a string as input and creates 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 the URL Helper of Codeigniter, which shall be loaded using :

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

    Ps: I know a lot about programming (I’m still in that difficult phase of learning) this was one of the only questions that I happen to know the solution for having been through the same problem, but if there is something wrong I stay open to improve the answer. ;)

  • I just think I missed a "little" there in the first sentence ein

  • @Andreyhartung Faltou even hahaha,had not even noticed! Now there is no way to edit more! Anyway thanks! ;)

4

There is, use it strtolowerand url_title.

strtolower : Returns string with all characters converted to lowercase.

url_title : Takes a string as input and creates 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 the 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 the 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)
}
  • 2

    I couldn’t remember the function url_title(); will be a hand on the wheel now, but complementing: There are accents in English (in a nutshell, but have) and in the helper Text IC, has exactly this function for accent removal: convert_accented_characters()

  • 2

    vlw @Jhonatanoliveira, I’ve already fixed my mistake

  • Wild-eyed!!!!

-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>";
  • Add some information to this reply

Browser other questions tagged

You are not signed in. Login or sign up in order to post.