Search code examples
phpregexurlseo

Remove all punctuation from php string for friendly seo url


So, I've seen a ton of "solutions" on this site, but none of them seem to work fully for me. I would like to strip all punctuation from a post name so that the system can dynamically create urls for each post. I found an article by David Walsh that provides a step by step tutorial on how this can be achieved. However, not everything gets stripped. Here is a link to the article (just in case): http://davidwalsh.name/php-seo.

Here's the code I've altered to remove all punctuation:

$return = trim(preg_replace('/[^a-z0-9]+/i'," ", strtolower($post_name)));

Here's an example post name: Testing's, this & more!

Results when I echo the url: testing-039-s-this-amp-more.php

I'm not sure why it's keeping the html code for the ampersand and the single quote. Any ideas?!?


Solution

  • Looks like the data is run through htmlspecialchars() or htmlentities() somewhere. Undo that with htmlspecialchars_decode() or html_entity_decode() first:

    $return = trim(preg_replace('/[^a-z0-9]+/i'," ", strtolower(htmlspecialchars_decode($post_name))));