***Generate URL from text***

by 16 replies
18
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;
 
      }
#programming #generate #text #url
    • [1] reply
    • 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.
      • [1] reply
  • Check the third comment from the top at PHP: preg_replace - Manual

    Is it something like that you are looking for?
  • 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)
  • 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('À', 'Á', 'Â', 'Ã', 'Ä', '...', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', ''', ''', '"', '"', 'Õ', '-', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '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 '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)));
    }
    ?>
    • [1] reply
    • Yes I did but this does not work for me....

      Only that code works what I mentioned above! And that need to be modify!


  • 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
    • [1] reply
    • 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.
  • ...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?
    • [1] reply
    • 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.
  • Here is the function page instead of the overview.

    PHP: iconv - Manual
  • Yes, I too suggest iconv function
  • 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?
  • 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.
  • Exactly... I'm also intrigued by this.

    How's the progress for you Peter?

Next Topics on Trending Feed