user1638055
user1638055

Reputation: 492

PHP: Removing various chars from a string - the shortest way?

I have a string, it contains umlauts and other chars I want to replace, right now my code looks like that:

$string = preg_replace('/( +)|([()])|(\/)/', '_', $string);
$string = preg_replace('/_{1,}/', '_', $string);
$string = preg_replace('/_$/', '', $string);
$string = str_replace('ö', 'oe', $string);
$string = str_replace('ü', 'oe', $string);
$string = str_replace('ä', 'oe', $string);
$string = str_replace('Ä', 'Ae', $string);
$string = str_replace('Ü', 'Ue', $string);
$string = str_replace('Ö', 'Oe', $string);

Is there a shorter way of doing that?

Thanks :)

Upvotes: 1

Views: 703

Answers (3)

hakre
hakre

Reputation: 198179

Yes there is. One thing is if that replacement represents something in your application (e.g. creating a slug out of a string: string_slug) you should first of all wrap it into a function of it's own which is pretty straight forward by taking the input variable and return the output:

function string_slug($string) {

    $string = preg_replace('/( +)|([()])|(\/)/', '_', $string);
    $string = preg_replace('/_{1,}/', '_', $string);
    $string = preg_replace('/_$/', '', $string);

    $string = str_replace('ö', 'oe', $string);
    $string = str_replace('ü', 'oe', $string);
    $string = str_replace('ä', 'oe', $string);
    $string = str_replace('Ä', 'Ae', $string);
    $string = str_replace('Ü', 'Ue', $string);
    $string = str_replace('Ö', 'Oe', $string);

    return $string;
}

As you can see, next to moving the code into the function body, I also grouped certain lines by the function they call. But that only as a side-note.

The usage of that replacement function is then less burdensome and straight forward:

$string = string_slug($string);

Can you imagine a less perfect way to do your string operation? Just a single line and the function is perfect for what you want to do.

Next to that you have asked on how to improve the replacement operations. That is merely knowledge and there are many, many ways to do that.

First of all you can improve your knowledge about the two functions you use. That could show you for example how you could improve the regular expression to condense it as well as that str_replace can work with arrays (@Kolnik gives an example here, however the order of operations differs to your original code).

Next to that, the str_replace part is doing something called like a translation table. Replace one with the other. A function in PHP that supports this is called strtrDocs.

Also take care that you save the PHP file with the right encoding otherwise the umlaut replacements (Ä etc.) might not work in your case.

Also you might want to learn about something called transliteration­Wikipedia.

Upvotes: 0

Brian
Brian

Reputation: 8626

The 'most correct' way is use PHP's iconv( Convert string to requested character encoding) functionality...

Example function to do exactly what you want: http://php.net/manual/en/function.iconv.php#83238

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324760

You could use str_replace with an array:

$find = Array(" ","(",")","/","ö","ü","ä","Ä","Ü","Ö");
$replace = Array("_","_","_","_","oe","oe","oe""Ae","Ue","Oe");
$string = str_replace($find,$replace,$string);
$string = preg_replace("/__+/","_",$string);
$string = rtrim($string,"_");

Upvotes: 2

Related Questions