Is there a simple'r way to regex this
Posted: Wed Oct 14, 2009 8:09 am
Hi guys,
Regex is not my first or 2nd language
Im trying to clean up form data $_POST['pagename'], for a CMS page name field.......ie, remove non-alphanumeric characters, double spaces/hyphens and tags etc, and replace space and underscores with hyphens
So far i have this and it works, but is there a cleaner way to do it
Thanks in advance
Regex is not my first or 2nd language
Im trying to clean up form data $_POST['pagename'], for a CMS page name field.......ie, remove non-alphanumeric characters, double spaces/hyphens and tags etc, and replace space and underscores with hyphens
So far i have this and it works, but is there a cleaner way to do it
Code: Select all
//string used --> Some test_txt^ with %$ symbols and 1233 numbers<p>`\'\"</p>
$MyStr = (string) trim(strip_tags($_POST['pagename']));
$MyStr = preg_replace('/[^a-zA-Z0-9\s-_]/', '', $MyStr);
$MyStr = str_replace('_', ' ', $MyStr);
$MyStr = preg_replace('/\\s/', '-', $MyStr);
echo $MyStr;
// result is --> Some-test-txt-with-symbols-and-1233-numbers