0

I try to get rid of some characters in a string. The string will be submited by a form (method: POST). I need to get a lower case string. The Characters ä, ö, ü and ß should become ae, oe, ue and ss. All other special characters and whitespaces should be removed.

The string

Lorem_ipsum_Ö-ß &änyOther char 

should become

lorem-ipsum-oe-ss-aenyother-char 

This is my code:

$slug = strtolower($_POST['title']); $slug = trim($slug, '-'); $slug = preg_replace('/ä/g', 'ae', $slug); $slug = preg_replace('/ö/g', 'oe', $slug); $slug = preg_replace('/ü/g', 'üe', $slug); $slug = preg_replace('/ß/g', 'ss', $slug); $slug = preg_replace('/[^\w ]/g', '', $slug); $slug = preg_replace('/ +/g', '-', $slug); 

First of all, my attempt doesn't work as I want to.

And second: How can I make this code smaller? I guess it is possible to get this in just one regular expression...

6
  • Please explain "does not work" Commented Sep 8, 2014 at 20:00
  • I don't get a result. Commented Sep 8, 2014 at 20:01
  • no /g modifier for preg_match, remove it, will work fine. Commented Sep 8, 2014 at 20:02
  • Oh. Quite simple :-) Thanks. Commented Sep 8, 2014 at 20:05
  • Ö will not transform to lowercase using strtolower() Commented Sep 8, 2014 at 20:07

2 Answers 2

1

You could always create a function to do this. You can tweak this to your needs, here is an example.

function _replace($t) { $map = array( 'Ä' => 'ae', 'ä' => 'ae', 'ß' => 'ss', 'Ö' => 'oe', 'ö' => 'oe', 'Ü' => 'ue', 'ü' => 'ue' ); $slug = strtr(strtolower(trim($t)), $map); return preg_replace('~[\W_]+~', '-', $slug); } 

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

Just one note to this: In my opinion you should use regex when you have to use regex. Otherwise it makes your code senselessly complicated. I honestly don't think that this is a use-case for a regex.
1

There is no /g modifier for preg_match. You can use preg_match_all or you can just remove it.

Here's your eval.in

Conversely, if you're just looking to make cleaner URL's out of this and you're trying ot minimize the code into a 1-liner, we could use iconv with TRANSLIT to translate the charset to the closest charset of our choosing, such as UTF-8, then you could wrap it all up into a 1 liner.

$slug = strtolower(urlencode(preg_replace('/[\s\-]/', '', iconv('utf-8', 'ascii//TRANSLIT',$str)))); 

Here's the eval.in for this example

1 Comment

Is it possible to optimize the code? Or do I have to take eight single lines?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.