This PHP function will use Regex (Regular expression) to remove all special characters and replace space with hyphens so your string only has letters and numbers. And then converts them all to lowercase. It makes your link friendly, good for SEO and great for naming your images/photos, url and attached file when publish them online.
PHP Function
<?php function clean($string) { $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens. $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars. $string = strtolower($string); // Convert to lowercase return $string; } ?> |
Usage
<?php $str_input = "PHP Remove All Special Characters And Replace Spaces With Hyphens | 4 Rapid Development"; $str_input = clean($string); echo $str_input; //php-remove-all-special-characters-and-replace-spaces-with-hyphens--4-rapid-development ?> |