I'm trying to strip all non-number, non-word characters from a string (except hyphens) to create a URI stub. I have the following code:
$urlPrefix = strtolower($this->urlPrefix.(substr($this->urlPrefix, -1) == '-' ? '' : '-'));
$urlPrefix = preg_replace('#\s+#', '-', $urlPrefix);
var_dump($urlPrefix);
$urlPrefix = preg_replace('#[^\d\w\s-]#g', '', $urlPrefix);
var_dump($urlPrefix); exit;
If I set $this->urlPrefix = "Test replace$%^*";
then the first var_dump($urlPrefix)
outputs test-replace$%^*-
as expected, but the second dump is outputting NULL
, but I can't see anything wrong with my regexp, can someone please help me? Why is preg_replace
erroring?