PHP cleaning strings with “preg_replace()” function and more resources for REGEX

Cleaning strings by removing anything that is not a letter or number can be very useful, like building clean URLs based on customer data.

This will replace anything that isn’t a letter, number or space. Then, you can further clean the spaces by replacing with dashes (“-“) for better URL readability.

Note that “preg_replace” uses the REGEX patterns and matching rules which is also used in .htaccess files. If sometimes you struggle with getting REGEX right, visit this excellent REGEX Cheatsheet: http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

Aaron Belchamber has written 243 articles

Leave a Reply

Your email address will not be published. Required fields are marked *