How to remove carriage returns from output of string?
I don't fully understand your exact problem, but the answer to the title of your question is quite simple:
$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.
I always use this to get rid of pesky carriage returns:
$string = str_replace("\r\n", "\n", $string); // windows -> unix$string = str_replace("\r", "\n", $string); // remaining -> unix
if you need to remove new line symbols of all types (in utf8)
$dataWithoutCarrierReturn = preg_replace('/\R/', '', $inData);