URL filtering is important: everyone from marketers to search engines loves nice, text-rich URLs. If we want to create keyword-rich URLs based on our dynamic content then we will need to clean and filter the source text first. 

Here is a handy function that will generate text very much. We can use this pretty much anywhere like in tagging systems, and in code that creates permalinks based on post titles or taxonomy terms.


function filter($value, $force_lower_case = true)
   if ($force_lower_case) {
    $value = strtolower($value);
   // remove everything except A-Z, a-z, 0-9, hyphens, and whitespace
   $value = preg_replace( "/[^a-zA-Z0-9-s]/", '', $value );
   // convert whitespaces to hyphens
   $value = preg_replace( "/s/", '-', $value );
   // replace multiple hyphens with a single hyphen
   $value = preg_replace( "/[-]+/", '-', $value );
   return $value;


This is a secure approach, though it will by default not allow some characters that are valid in URLS. The solution here is a good basic one, but if we do want to allow other characters we should do some research before adding them to the white list.
Bear in mind that the three regular expressions here will have an overhead of performance cost.
Because of the regular expressions, this code is best suited for running once when writing to the database, and is not ideal for running on each page load.

Thank You!!!

Contact Us On WhatsApp