SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Berlin
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    change title to a URL compliant string

    Hello,

    How could I change a page title to a URL compliant string?

    For example:
    $title = "Gänseleber & Gemüsespieße?"

    should become
    ganseleber_gemusespiesse

    or better

    gaenseleber_gemuesespiesse

    Replacing known characters seems simple to me, but how assure that only letters, numbers and a underline [a-z0-1_] will be used? Any other and unforeseeable character should be ignored.


    Regards
    Flözen

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,033
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    I would use the urlencode() function instead of adding a custom replace scheme:
    http://www.php.net/urlencode
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use iconv() with the //TRANSLIT option to change as many of the non ascii characters as possible to similar characters in ascii. Then you can use a regular expression to strip any remaining bad characters.

  4. #4
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Berlin
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks allot!
    iconv() was what I was looking for.

    With
    PHP Code:
    setlocale(LC_ALL'de_DE.UTF8');
    iconv("ISO-8859-1""ASCII//TRANSLIT""$titel"); 
    it works for German Umlaute.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •