Friendly URL's: replacing special Latin characters, lowercase

Discussion in 'PHP' started by James_DG, Jan 8, 2014.

  1. #1
    I'm completely stuck with the codes below. Have been trying for days now, without any luck:

    Current situation My url's for my mobile websites are displayed with dashes, when page titles include:
    • symbols such as ?!
    • special Latin characters such as ñéáâç
    Current outcome: In other words, for example the url with a page title ¿Por qué mañana? gets displayed as: m.mywebsite.com/-Por-qu--ma-ana-.php

    Desired situation
    • remove symbols such as ?!
    • replace special Latin characters by their "normal" equivalents, for instance ñ>n é>e ç>c
    • replace uppercase letters by lower case letters. That would also mean: É>e Ñ>n
    Desired outcome: In other words, the desired url structure for the same example would be: m.mywebsite.com/por-que-manana.php

    Code

    if($page->type=='page'){
    $page->url = remove_accents($page->title).".php";
    if($page->parent > 1){
    $l = getParentById($page->parent,$pages);
    $page->url = remove_accents($l->title)."/".$page->url;
    }
    //print_r($page);
    //exit;

    Function
    function remove_accents($string){
    $table = array('Š'=>'S','š'=>'s','Ð'=>'Dj','ð'=>'dj','Ž'=>'Z','ž'=>'z','?'=>'C','?'=>'c','?'=>'C','?'=>'c','À'=>'A','Á'=>'A','Â'=>'A','Ã'=>'A','Ä'=>'A','Å'=>'A','Æ'=>'A','Ç'=>'C','È'=>'E','É'=>'E','Ê'=>'E','Ë'=>'E','Ì'=>'I','Í'=>'I','Î'=>'I','Ï'=>'I','Ñ'=>'N','Ò'=>'O','Ó'=>'O','Ô'=>'O','Õ'=>'O','Ö'=>'O','Ø'=>'O','Ù'=>'U','Ú'=>'U','Û'=>'U','Ü'=>'U','Ý'=>'Y','Þ'=>'B','ß'=>'Ss','à'=>'a','á'=>'a','â'=>'a','ã'=>'a','ä'=>'a','å'=>'a','æ'=>'a','ç'=>'c','è'=>'e','é'=>'e','ê'=>'e','ë'=>'e','ì'=>'i','í'=>'i','î'=>'i','ï'=>'i','ð'=>'o','ñ'=>'n','ò'=>'o','ó'=>'o','ô'=>'o','õ'=>'o','ö'=>'o','ø'=>'o','ù'=>'u','ú'=>'u','û'=>'u','ý'=>'y','ý'=>'y','þ'=>'b','ÿ'=>'y','?'=>'R','?'=>'r',);

    return strtr($string, $table);}

    The function above seems to work and replaces the special Latin characters with their "normal" versions. Now, I would like them to be (1) all lowercase and (2) remove question marks and exclamation marks (also the Spanish variants, so: ¿?¡!) - so it meets the desired outcome mentioned above.

    The first can be done with "strtolower" but I don't know how to combine it with the function above. Same holds for removing question marks and exlamation marks

    Can somebody help me out, please? What would be the total code/function? I'm new to this, and I'm completely stuck now for a few days. Many thanks in advance!
     
    James_DG, Jan 8, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    9You could redo the whole function like this:
    
    function change_url_char($string) {
    $string = strtolower($string);
    $replace_these_chars = array('?','!','¿','¡','š','ð','ž','à','á','â') //this needs to be completed with the rest of the characters that need replacing
    $replace_with_these_chars = array('','','','','s','dj','z','a','a','a') // and this needs a replacement character for each character in the first array
    $string = str_replace($replace_these_chars,$replace_with_these_chars,$string);
    return $string;
    }
    
    PHP:
    All you need to do with the function above is add the desired characters to the arrays
     
    PoPSiCLe, Jan 8, 2014 IP
  3. James_DG

    James_DG Greenhorn

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Many thanks for your reply! As mentioned I have no experience with PHP, so thinking of another function only makes me more confused. :)

    I managed to display all letters lowercase. However, now spaces between two words are not replaced anymore with a "-".

    So basically, the only things missing are:
    1) replace spaces with "-
    2) remove "¿", "?", "¡", "!"

    The function belows seems to meet these conditions.
    function remove_accents($string)
      {
     
      $table = array(
      'Š' => 'S', 'š' => 's', 'Ð' => 'Dj', 'ð' => 'dj', 'Ž' => 'Z', 'ž' => 'z', '?' => 'C', '?' => 'c', '?' => 'C', '?' => 'c',
      'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'A', 'Ç' => 'C', 'È' => 'E', 'É' => 'e',
      'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', 'Ñ' => 'n', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O',
      'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ý' => 'Y', 'Þ' => 'B', 'ß' => 'Ss',
      'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'a', 'ç' => 'c', 'è' => 'e', 'é' => 'e',
      'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'o', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o',
      'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ý' => 'y', 'ý' => 'y', 'þ' => 'b',
      'ÿ' => 'y', '?' => 'R', '?' => '?',
      );
      return strtr($string, $table);
      }
      $string = remove_accents($string);
      $string = strtolower($string);
      $string = preg_replace('#[^a-z0-9]+#', '-', $string);
      $string = trim($string, '-');
      echo $string; 
    PHP:

    How would I integrate the above in the code below?

    My edited code:

     if($page->type=='page'){
      $page->url = strtolower ( remove_accents($page->title).".php");
      if($page->parent > 1){
      $l = getParentById($page->parent,$pages);
      $page->url = strtolower ( remove_accents($l->title)."/".$page->url);
      }
    
    PHP:
    I'm nearly there, and I guess this is pretty easy for advanced users. Thanks in advance!!
     
    Last edited: Jan 8, 2014
    James_DG, Jan 8, 2014 IP
  4. James_DG

    James_DG Greenhorn

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    Problem has been solved! The codes below did the trick!

    Code

    if($page->type=='page'){
    $page->url = remove_accents($page->title).".php";

    if($page->parent > 1){
    $l = getParentById($page->parent,$pages);
    $page->url = remove_accents($l->title)."/".$page->url;
    }

    Function
    function remove_accents($string)
    {

    $table = array(
    'Š' => 'S', 'š' => 's', 'Ð' => 'Dj', 'ð' => 'dj', 'Ž' => 'Z', 'ž' => 'z', '?' => 'C', '?' => 'c', '?' => 'C', '?' => 'c',
    'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'A', 'Ç' => 'C', 'È' => 'E', 'É' => 'e',
    'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', 'Ñ' => 'n', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O',
    'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ý' => 'Y', 'Þ' => 'B', 'ß' => 'Ss',
    'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'a', 'ç' => 'c', 'è' => 'e', 'é' => 'e',
    'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'o', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o',
    'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ý' => 'y', 'ý' => 'y', 'þ' => 'b',
    'ÿ' => 'y', '?' => 'R', '?' => '?',
    );

    $string = strtr($string, $table);
    $string = strtolower($string);
    $string = preg_replace('#[^a-z0-9/]+#', '-', $string);
    $string = trim($string, '-');
    return $string;
    }
     
    James_DG, Jan 8, 2014 IP