Stripping string of URL unsafe characters

Stripping string of URL unsafe characters
A function that will strip all the unsafe characters from a string in order to make it valid for URLs. This can be especially useful when you create your own URLs dynamically using mod_rewrite.
1.  function StripUrl($title)
2.  {
3.          $title = str_replace("#", "sharp", $title);
4.          $title = str_replace("/", "or", $title);
5.          $title = str_replace("$", "", $title);
6.          $title = str_replace("&", "and", $title);
7.          $title = str_replace("&", "and", $title);
8.          $title = str_replace("+", "plus", $title);
9.          $title = str_replace(",", "", $title);
10.         $title = str_replace(":", "", $title);
11.         $title = str_replace(";", "", $title);
12.         $title = str_replace("=", "equals", $title);
13.         $title = str_replace("?", "", $title);
14.         $title = str_replace("@", "at", $title);
15.         $title = str_replace("<", "", $title);
16.         $title = str_replace(">", "", $title);
17.         $title = str_replace("%", "", $title);
18.         $title = str_replace("{", "", $title);
19.         $title = str_replace("}", "", $title);
20.         $title = str_replace("|", "", $title);
21.         $title = str_replace("\\", "", $title);
22.         $title = str_replace("^", "", $title);
23.         $title = str_replace("~", "", $title);
24.         $title = str_replace("[", "", $title);
25.         $title = str_replace("]", "", $title);
26.         $title = str_replace("`", "", $title);
27.         $title = str_replace("'", "", $title);
28.         $title = str_replace("\", "", $title);
29.         $title = str_replace("_", "-", $title);
30.         $title = str_replace(" ", "-", $title);
31.         return $title;
32. }
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

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

Back To Top