Remove characters (dots spaces tables new lines carriage returns whitespace) from a string
<?php
// http://stackoverflow.com/questions/6442174/replace-all-characters-except-letters-numbers-spaces-and-underscores
// replace all characters with a hyphen, except alphanumeric characters
$target_file = preg_replace('/[^0-9a-zA-Z]/', '-', $target_file)
// http://stackoverflow.com/questions/3059091/how-to-remove-carriage-returns-from-output-of-string
$snip = str_replace('.', '', $snip); // remove dots
$snip = str_replace(' ', '', $snip); // remove spaces
$snip = str_replace("\t", '', $snip); // remove tabs
$snip = str_replace("\n", '', $snip); // remove new lines
$snip = str_replace("\r", '', $snip); // remove carriage returns
// Or a all in one solution:
$snip = str_replace(array('.', ' ', "\n", "\t", "\r"), '', $snip);
// You can also use regular expressions:
$snip = preg_replace('~[[:cntrl:]]~', '', $snip); // remove all control chars
$snip = preg_replace('~[.[:cntrl:]]~', '', $snip); // above + dots
$snip = preg_replace('~[.[:cntrl:][:space:]]~', '', $snip); // above + spaces
// You'll still need to use addslashes() to output $snip inside Javascript.
// ----------------------
// http://stackoverflow.com/questions/4300440/php-removing-excess-whitespace
// With a regexp :
preg_replace('/( )+/', ' ', $string);
// If you also want to remove every multi-white characters, you can use \s (\s is white characters)
preg_replace('/(\s)+/', ' ', $string);
// ----------------------
// http://stackoverflow.com/questions/5245513/php-preg-replace-preg-match-vs-php-str-replace
// str_replace replaces a specific occurrence of a string, for instance "foo"
// will only match and replace that: "foo". preg_replace will do regular
// expression matching, for instance "/f.{2}/" will match and replace "foo",
// but also "fey", "fir", "fox", "f12", etc.
$string = "foo fighters";
$str_replace = str_replace('foo','bar',$string);
$preg_replace = preg_replace('/f.{2}/','bar',$string);
echo 'str_replace: ' . $str_replace . ', preg_replace: ' . $preg_replace;
// the output is:
// str_replace: bar fighters, preg_replace: bar barhters
?>