Can you simplify this function? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Can you simplify this function? (/showthread.php?tid=8421) |
Can you simplify this function? - El Forum - 05-17-2008 [eluser]codex[/eluser] I have this function for normalizing tags before writing them to the database. It works, that's not the issue, but I was just wondering if it's possible to shorten or simplify the sucker. If not clear, what it does is it strips characters like '!@#$%^&*(' and replaces characters like 'éèëê' with e, 'ÀÁÂÃ' with A etc. Code: function normalize_tag($s) { Can you simplify this function? - El Forum - 05-17-2008 [eluser]TheLoops[/eluser] If by "Replace multiple spaces/divisions/underscores" you mean "only if > 1 are found"… … then try this combination: Code: $s = preg_replace('/( {2,}|-{2,}|_{2,})/', '_', $s); // Replace multiple spaces/divisions/underscores with a single underscore … else try this: Code: $s = preg_replace('/[ \-_]+/', '_', $s); // Replace multiple spaces/divisions/underscores with a single underscore Can you simplify this function? - El Forum - 05-17-2008 [eluser]Pascal Kriete[/eluser] Here's my take on the thing. It's a little shorter and deals with some exceptions. Not tested though, so no guarantees . Code: // This way we don't have to mess with uppercase the rest of the time Can you simplify this function? - El Forum - 05-17-2008 [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 . Code: // This way we don't have to mess with uppercase the rest of the time 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! Can you simplify this function? - El Forum - 05-18-2008 [eluser]xwero[/eluser] Nice use of htmlentities/html_entity_decode to catch accented chars. I think those cover German, French, Dutch, Spanish and Scandinavic languages. |