0

I'm looking to modify a PHP string so I can use it as an anchor tag.

I used the method found here: Remove all special characters from a string

It worked well to remove ampersands from my strings, but it doesn't seem to be removing or affecting the brackets or punctuation.

Here's what I'm currently using:

$name_clean = preg_replace('/ [^A-Za-z0-9\-]/', '', $name); // REMOVES SPECIAL CHARACTERS
$name_slug = str_replace(' ', '-', $name_clean); // REPLACES SPACES WITH DASHES IN TITLE
$link = strtolower( $name_slug ); // CREATES LOWERCASE SLUG VERSION OF TITLE_SLUG

My string (in this case $name) = St. John's (Newfoundland).

The output I get = #st.-john'snewfoundland)

I'd like to remove the periods, apostrophes and brackets altogether.

Any help would be greatly appreciated!

Community
  • 1
  • 1
Eric Wood
  • 579
  • 2
  • 9
  • 22
  • Is the leading space in the regex intentional? – apokryfos May 17 '16 at 15:31
  • It was, because originally the only character I was trying to affect was an ampersand that always followed a space. With a few adjustments I was able to get it working though. Thanks! – Eric Wood May 17 '16 at 15:38

1 Answers1

2

Your regex pattern / [^A-Za-z0-9\-]/ appears to contain a space after the opening /. This pattern will only match a special character that comes after a space. Removing that space should get the result you want.

Joe
  • 25,000
  • 3
  • 22
  • 44