I'm testing a preg_replace function, and I return from an ajax function the processed data (after I process the data through preg_replace, I put it through htmlentities() ):
My test string is:
pr eg123 ~!@#$%^&*()-+={}|[]:;< >? "...,'/...
I'm trying to make sure all those characters aren't replaced. My replace function is:
$string = preg_replace('/[^a-zA-Z0-9\s+\n\r,.\/~!@#\$%\^&*()\+={}\[\]|:;<>?\'"-]/', '', $string);
I return both the data from "echo" and after going through htmlentities() to see the difference. when I return the data using alert(data), I get:
pr eg123 ~!@#$%^&*()-+={}|[]:;< >? "...,'/...
pr eg123 ~!@#$%^&*()-+={}|[]:;< >? "...,'/...
respectively. However, when I put either of those into $("#div").html(data), I get:
pr eg123 ~!@#$%^&*()-+={}|[]:;< >? "...,'/...
so the multiple spaces are lost. Why does the .html() function reduce the spaces? And how can I fix this? Thanks
` tags or the css `white-space` seems to be the only way to preserve spaces. As an aside comment, your preg_replace can be shorten: `preg_replace('/[^]!-[\^a-~\s]+/', '', $string);`– Casimir et Hippolyte Apr 22 '14 at 02:34