PHP Character removal tool

Written by: John | 30th Jun 2014

While developing a php based on-line shop I came across a neat little function to remove those pesky unwanted chars from post or get type mysql queries using str_replace() & an array(). But this little function proves quite useful for all kinds of character removal situations. This function is no way revolutionary, unique or new, but never the less it is very useful so I thought I would share it.

The function:

<?php
function cl_str($in_str) { 
   // can be any char(s) you like 
   $to_remove= array("#","%","&gt;","&lt;", "/", "[", "]", "?", "-");
   $new_str = str_replace($to_remove, "", $in_str); 
   return $new_str; 
}
?>

The usage:

<?php 
   $some_text = "<blah blah ?-->";
   $new_text = cl_str($some_text);
   echo $new_text;
   //returns "blah blah"
?>

 

Related Posts

Code

Swipe close for mobile menu

Using Vanilla JS (ish) to add swipe features to mobile views.
Code

Full Width YouTube Videos

Make YouTube videos take the full width of an element while retaining the 16:9 ratio.
Code

CSS Transitions

Easy copy and paste transition css, to enable transitions to all properties.
Code

Command Line SSH stuff

Some tips and tricks for using the command line to do useful stuff, mainly just a reference for myself.
Code

Handy, dandy WordPress excerpt length function

If you have been using WordPress on multiple projects you must have come up against needing to pull a custom excerpt size? Now there are many ways to do this, eg, you could use this to pull say 80 characters…