Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3790316
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:17:44+00:00 2026-05-19T12:17:44+00:00

I have strings with special characters I want to convert. Usually I find all

  • 0

I have strings with special characters I want to convert. Usually I find all the special characters and their “web safe” counter parts manually and put them in arrays. Then I use preg_replace to replace each of the characters.

But I cannot help but thinking that there is an easier solution since it is error prune approach.

Here is an example of what I want:

Hans Günther -> hans-gunther
Jären höst   -> jaeren-hoest
René Ågesen  -> rene-aagesen
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T12:17:45+00:00Added an answer on May 19, 2026 at 12:17 pm

    string urlencode ( string $str )

    http://php.net/manual/en/function.urlencode.php

    Actually here is a great post about converting text like your example above to nice url-safe strings (probably better for you than the above function):

    http://cubiq.org/the-perfect-php-clean-url-generator

    setlocale(LC_ALL, 'en_US.UTF8');
    function toAscii($str, $replace=array(), $delimiter='-') {
     if( !empty($replace) ) {
      $str = str_replace((array)$replace, ' ', $str);
     }
    
     $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
     $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
     $clean = strtolower(trim($clean, '-'));
     $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
    
     return $clean;
    }
    

    Here are examples of what it does:

    echo toAscii("Mess'd up --text-- just (to) stress /test/ ?our! `little` \\clean\\ url fun.ction!?-->");
    returns: messd-up-text-just-to-stress-test-our-little-clean-url-function
    
    echo toAscii("Perché l'erba è verde?", "'"); // Italian
    returns: perche-l-erba-e-verde
    
    echo toAscii("Peux-tu m'aider s'il te plaît?", "'"); // French
    returns: peux-tu-m-aider-s-il-te-plait
    
    echo toAscii("Tänk efter nu – förr'n vi föser dig bort"); // Swedish
    returns: tank-efter-nu-forrn-vi-foser-dig-bort
    
    echo toAscii("ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöùúûüýÿ");
    returns: aaaaaaaeceeeeiiiidnooooouuuuyssaaaaaaaeceeeeiiiidnooooouuuuyy
    
    echo toAscii("Custom`delimiter*example", array('*', '`'));
    returns: custom-delimiter-example
    
    echo toAscii("My+Last_Crazy|delimiter/example", '', ' ');
    returns: my last crazy delimiter example
    

    If you want to convert something like ä to ae and etc you can use a script like this (sorry, don’t know about a better way of doing this):

    setlocale(LC_ALL, 'de_DE');
    $replace = array(
     'illegal' => array('/Ä/', '/Ö/', '/Ü/', '/ä/', '/ö/', '/ü/', '/Â/', '/é/'),
     'legal' => array('Ae', 'Oe', 'Ue', 'ae', 'oe', 'ue', 'Aa', 'e')
     );
    $string = 'ich hätte gerne brötechen Mein Name ist Öles Âlex';
    echo preg_replace($replace['illegal'], $replace['legal'], $string);
    
    //Output: "ich haette gerne broetechen Mein Name ist Oeles Aalex"
    

    You can obviously put it all together like this (only converting ü->ue etc, just add more to the first preg_replace):

    setlocale(LC_ALL, 'en_US.UTF8');
    function toAscii($str, $replace=array(), $delimiter='-') {
        if( !empty($replace) ) {
            $str = str_replace((array)$replace, ' ', $str);
        }
    
     $clean = preg_replace(array('/Ä/', '/Ö/', '/Ü/', '/ä/', '/ö/', '/ü/'), array('Ae', 'Oe', 'Ue', 'ae', 'oe', 'ue'), $str);
        $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $clean);
        $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
        $clean = strtolower(trim($clean, '-'));
        $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
    
        return $clean;
    }
    
    $text = "Hätten Sie gerne viele Brötchen? Wenn ja dann einfach *!@#$%^&*()eingeben...";
    echo toAscii($text);
    //OUTPUT: haetten-sie-gerne-viele-broetchen-wenn-ja-dann-einfach-eingeben
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a lot of strings of special characters. I want to replace all
I have a string with special characters in Java. [^\\]*\\ I want to convert
If you have a string with special characters that you want to match with:
Ok, I understand that using strings that have special characters is an encoding issue.
I have a string with bunch of special characters/symbols and I want to replace
I want to replace special characters like &,%,$ to string. I have a string
I want to remove all special characters from a string. Allowed characters are A-Z
I have certain strings which contain special characters so they can not be shared
I have a string with special characters like this: äöüß& Now I want to
I have a bunch of string with special escape codes that I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.