Welcome Guest, Not a member yet? Register   Sign In
Can you simplify this function?
#4

[eluser]codex[/eluser]
[quote author="inparo" date="1211055482"]Here's my take on the thing. It's a little shorter and deals with some exceptions. Not tested though, so no guarantees Smile .
Code:
// This way we don't have to mess with uppercase the rest of the time
$s = strtolower(htmlentities($s));

// We don't need to capture the second group, so I made it optional || added slash for oslash
$s = preg_replace ('/&([a-z])(?:uml|acute|grave|circ|tilde|cedil|ring|slash);/', '$1', $s);

// Weird characters that don't get caught above - also includes ðand þ, but I don't know what the best replacement for those would be.
// While we're at it, we'll also get the http
$s = str_replace( array('ß', 'æ', 'http://'), array('ss', 'ae', ''), $s);

$s = html_entity_decode($s);

// Normalize multiple spaces, dashes, and underscores
$s = preg_replace( array('/\s+/', '/-+/', '/_+/'), '_', $s);

// I've added a space here
$s = preg_replace('/[^a-z 0-9_-]/', '', $s);

return $s;
[/quote]

That works indeed. It's not spectacularly shorter (which I hoped was possible), but you shaved off a few bits, which is always good. Plus I learned something new in the process. So thank you!


Messages In This Thread
Can you simplify this function? - by El Forum - 05-17-2008, 08:03 AM
Can you simplify this function? - by El Forum - 05-17-2008, 08:56 AM
Can you simplify this function? - by El Forum - 05-17-2008, 09:18 AM
Can you simplify this function? - by El Forum - 05-17-2008, 05:59 PM
Can you simplify this function? - by El Forum - 05-18-2008, 12:06 AM



Theme © iAndrew 2016 - Forum software by © MyBB