I think I know what the below is doing, but not wanting to make any assumptions wanted to make sure..
RegEx is:
$value = preg_replace(array('/[^a-z0-9_ -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), $str);
First part in the array is simple, replaces anything but a-z, 0-9, underscore, space and a hyphen with nothing. Though shouldn’t the hyphen after the space be escaped?
Second part I’m not so sure…. I assume it’s saying replace one or more spaces OR one or more hyphens with a single hyphen? Am I right? But again, escape the hyphen?
Third part is a little confusing….. isn’t the | character in regex the OR operator? So replace a hyphen or a hyphen with nothing? If the | was escaped then I would read it as the whole string would need to match -|- and replace it with nothing?
Lastly….. am I correct in my thinking that it handles the replacements in the position it gets them? So it would handle the first spot in the array first and then move onto the second and third with what’s left of the string; so if the first replacement for example replaces something, then the second replacement will work on the revised version of the string?
After the first replacement, the string will only have letters, numbers, spaces, underscores, and hyphens. The second replacement will turn all consecutive spaces and/or hyphens into a single hyphen, and the third replacement will remove a hyphen from the beginning or the end of the string.
Other comments:
/^-|-$/has the anchors^and$, which mean beginning of string and end of string, respectively, so this regex will match a single hyphen but only if it is at the beginning or end of the string.