I am trying to remove all extra whitespace characters from a string in php. The string source is from an xml feed of customer reviews.
A few reviews have just too much whitespace and they are showing up in my html! Whether that be… The spacebar being hit too many times, the carriage return, or a newline, I just want a maximum of one space ' ' between characters.
$review = str_replace(array('\n','\r\n',PHP_EOL,'\0','\t'), '', $reviewText[1]);
$review = trim(ereg_replace(' +', ' ', $review));
I have also tried using preg_replace('/\s\s+/', ' ', $review); instead of the trim(ereg_replace( line
Despite my best efforts, I still can’t get this invisible whitespace / newline character to disappear.
Any ideas?
I have pasted the review below with xxx to replace any kind of identifying information. This is the output from a var_dump($review);
Here is the review below:
rtrim()
ltrim()
trim()
These should do what you’re looking for.
trim('----')should output ”rtrim('----a----');should output ‘—-a’ltrim('----a----');should output ‘a—-‘Each hyphen represents a space.
EDIT:
And after I read your post 🙂
Is the new line a break line (
<br>, <br />etc)?print_rdoesn’t convert text to htmlentities