***Generate URL from text***

16 replies
Hi,

I have a code here which actually generate url from my articles title from a directory script.

The questin is how can I make it to replace words like áóíúé etc... (iso-8859-2) characters into a friendly url. So from "á" will be "a" etc..

At the moment all these foriner characters getting replaced with " - "

Can anyone help me to figure out this issue? Thx.

Here is the code:

Code:
  
      function generateUrl($title) {
      $title= preg_replace('/[^A-Za-z0-9-]/', ' ', $title);
      $title= preg_replace('/ +/', ' ', $title);
      $title= trim($title);
      $title= str_replace(' ', '-', $title);
      $title= preg_replace('/-+/', '-', $title);
      $title= strtolower($title);
      return $title;
 
      }
#generate #text #url
  • {{ DiscussionBoard.errors[2023815].message }}
    • Profile picture of the author Peter Hupuczi
      Originally Posted by stma View Post

      Thanks for reply! However this does not resolved m any of my problem

      I'm still struggling with this issue.

      There is any programmer out there who have a minute to have a look at this please??

      Thanks.
      {{ DiscussionBoard.errors[2026158].message }}
      • Profile picture of the author stma
        Originally Posted by Peter Hupuczi View Post

        Thanks for reply! However this does not resolved m any of my problem

        I'm still struggling with this issue.

        There is any programmer out there who have a minute to have a look at this please??

        Thanks.
        The iconv functions are used to clean up text.
        {{ DiscussionBoard.errors[2027070].message }}
  • Profile picture of the author Manfred Ekblad
    Check the third comment from the top at PHP: preg_replace - Manual

    Is it something like that you are looking for?
    {{ DiscussionBoard.errors[2026207].message }}
  • Profile picture of the author Peter Hupuczi
    Hi,

    What I'm looking for is how I need to modify the code below to replace iso-8859-2 characters eg.: á, í, ó, é, ú, Ã, Ã", É, etc... with a, i, o, e, A, O, etc..

    Here is the code:

    Code:
    function generateUrl() {
          = preg_replace('/[^A-Za-z0-9-]/', ' ', );
          = preg_replace('/ +/', ' ', );
          = trim();
          = str_replace(' ', '-', );
          = preg_replace('/-+/', '-', );
          = strtolower();
          return ;
     
          }
    This code at the moment the code simply leave out from the generated url all the characters mentioned above (iso-8859-2)(Hungarian)
    {{ DiscussionBoard.errors[2026260].message }}
  • Profile picture of the author Manfred Ekblad
    Hmm... did u read the comments on the link i posted? :p

    Here is some code i copy&paste from that page (thx to hello at weblap dot ro for the code):

    <?php
    function remove_accent($str)
    {
    $a = array('À', 'Ã', 'Â', 'Ã', 'Ä', 'Ã...', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'ÃŒ', 'Ã', 'ÃŽ', 'Ã', 'Ã', 'Ã'', 'Ã'', 'Ã"', 'Ã"', 'Õ', 'Ã-', 'Ø', 'Ù', 'Ú', 'Û', 'Ãœ', 'Ã', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'Ã¥', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', 'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', 'ĭ', 'Į', 'į', 'İ', 'ı', 'IJ', 'ij', 'Ĵ', 'ĵ', 'Ķ', 'ķ', 'Ĺ', 'ĺ', 'Ļ', 'ļ', 'Ľ', 'ľ', 'Ŀ', 'ŀ', 'Ł', 'ł', 'Ń', 'ń', 'Ņ', 'ņ', 'Ň', 'ň', 'ʼn', 'Ō', 'ō', 'Ŏ', 'ŏ', 'Ő', 'ő', 'Å'', 'Å"', 'Ŕ', 'ŕ', 'Ŗ', 'ŗ', 'Ř', 'ř', 'Ś', 'ś', 'Ŝ', 'ŝ', 'Ş', 'ş', 'Å ', 'Å¡', 'Ţ', 'ţ', 'Ť', 'ť', 'Ŧ', 'ŧ', 'Ũ', 'ũ', 'Ū', 'ū', 'Ŭ', 'ŭ', 'Ů', 'ů', 'Ű', 'ű', 'Ų', 'ų', 'Ŵ', 'ŵ', 'Ŷ', 'ŷ', 'Ÿ', 'Ź', 'ź', 'Ż', 'ż', 'Ž', 'ž', 'ſ', 'Æ'', 'Ơ', 'ơ', 'Ư', 'ư', 'Ǎ', 'ǎ', 'Ǐ', 'ǐ', 'Ǒ', 'ǒ', 'Ǔ', 'ǔ', 'Ǖ', 'ǖ', 'Ǘ', 'ǘ', 'Ǚ', 'ǚ', 'Ǜ', 'ǜ', 'Ǻ', 'ǻ', 'Ǽ', 'ǽ', 'Ǿ', 'ǿ');
    $b = 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', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
    return str_replace($a, $b, $str);
    }

    function post_slug($str)
    {
    return strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'),
    array('', '-', ''), remove_accent($str)));
    }
    ?>
    {{ DiscussionBoard.errors[2026282].message }}
    • Profile picture of the author Peter Hupuczi
      Originally Posted by Manfred Ekblad View Post

      Hmm... did u read the comments on the link i posted? :p

      Here is some code i copy&paste from that page (thx to hello at weblap dot ro for the code):

      <?php
      function remove_accent()
      {
      = array('À', 'Ã', 'Â', 'Ã', 'Ä', 'Ã...', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'ÃŒ', 'Ã', 'ÃŽ', 'Ã', 'Ã', 'Ã'', 'Ã'', 'Ã"', 'Ã"', 'Õ', 'Ã-', 'Ø', 'Ù', 'Ú', 'Û', 'Ãœ', 'Ã', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'Ã¥', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', 'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', 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e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
      return str_replace(, , );
      }

      function post_slug()
      {
      return strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'),
      array('', '-', ''), remove_accent()));
      }
      ?>
      Yes I did but this does not work for me....

      Only that code works what I mentioned above! And that need to be modify!
      {{ DiscussionBoard.errors[2026331].message }}
  • Profile picture of the author Manfred Ekblad


    im confused... lol

    could you tell me why it wouldnt work for you? do you have a limit to the amount of lines of code you are allowed to use? :p
    {{ DiscussionBoard.errors[2026394].message }}
    • Profile picture of the author Peter Hupuczi
      Originally Posted by Manfred Ekblad View Post



      im confused... lol

      could you tell me why it wouldnt work for you? do you have a limit to the amount of lines of code you are allowed to use? :p
      I think this is not as simple as it sounds...

      This is an article directory script and this function actually convert the title into url.

      It is fine in English language... But Since the site will operate in Hungary...

      That is whay I need this to be fixed. The directory script is from ArticleMS.
      {{ DiscussionBoard.errors[2026497].message }}
  • Profile picture of the author Manfred Ekblad
    ...so you have to keep all the modifications inline within your generateUrl($title)-function?

    could you please give some more details...

    ...and a simple question, do you need some help or do you need someone to actually write the complete function for you?
    {{ DiscussionBoard.errors[2026589].message }}
    • Profile picture of the author Peter Hupuczi
      Originally Posted by Manfred Ekblad View Post

      ...so you have to keep all the modifications inline within your generateUrl()-function?

      could you please give some more details...

      ...and a simple question, do you need some help or do you need someone to actually write the complete function for you?
      Yes I have to...

      The function is already written but for an unknown reason is not working. We could not figure out what can be the problem.

      I'm attached a file. The file called core.php contains the original function which generate url from the title. If you open it with Notepad++ have a look at line 3605. The function starting there.

      But is actually not works it does not convert anything.

      What I have done:

      I have changed the original function to this


      function generateUrl($title) {
      $title= preg_replace('/[^A-Za-z0-9-]/', ' ', $title);
      $title= preg_replace('/ +/', ' ', $title);
      $title= trim($title);
      $title= str_replace(' ', '-', $title);
      $title= preg_replace('/-+/', '-', $title);
      $title= strtolower($title);
      return $title;

      }
      But now, it just ignore all the iso-8859-2 characters.
      {{ DiscussionBoard.errors[2029590].message }}
  • Profile picture of the author stma
    Here is the function page instead of the overview.

    PHP: iconv - Manual
    {{ DiscussionBoard.errors[2027323].message }}
  • Profile picture of the author weaveronline
    Yes, I too suggest iconv function
    Signature

    Thanks & Regards,
    Reach us at dukeduke600@gmail.com.
    Web Design| Logo Design | Banner Design | Web Development | Mobile Applications [iPhone/iPad/Android/Windows Phone]

    {{ DiscussionBoard.errors[2027354].message }}
  • Profile picture of the author Manfred Ekblad
    Well, you could call one of the other functions from within that function and just have it return the string without the "strange" characters and then do the normal parsing.

    Or am I missing some crucial piece of information here?
    {{ DiscussionBoard.errors[2029669].message }}
  • Profile picture of the author Number_5
    Am I dense or is the attachment missing?

    Is your function within a class and can you post an example of you trying to use the conversion function that was already posted? From a brief look, it should do exactly what you are trying to accomplish, but you may not be implementing it correctly.
    {{ DiscussionBoard.errors[2039084].message }}
  • Profile picture of the author Manfred Ekblad
    Exactly... I'm also intrigued by this.

    How's the progress for you Peter?
    {{ DiscussionBoard.errors[2039468].message }}

Trending Topics