I have a sentence like this.
1 2 3 4
As you see, in between 1 2 and 3 text, there are extra spaces. I want the output with only one space between them. so my output will be 1 2 3 4
.
If I use trim, it can only remove white space, but not that
How can I use PHP trim function to get the output like this?
just remember you need to echo out the result of the str_replace and you alo dont need to worry about white spaces a the browser will only show one white space.
Found this at php.net, works great:
Source: http://php.net/manual/en/function.trim.php#98812
if your string actually has " ",
A more inclusive answer for those who want to just do a trim:
Same trim handling html entities:
This html_entity_decode and trim interaction is outlined in the PHP docs here: http://php.net/manual/en/function.html-entity-decode.php#refsect1-function.html-entity-decode-notes
A little late to answer but hopefully might help someone else. The most important while extracting content from html is to use utf8_decode() in php. Then all other string operations become a breeze. Even foreign characters can be replaced by directly copy pasting characters from browser into the php code. The following function replaces
with a space. Then all extra white spaces are replaced with a single white space usingpreg_replace()
. Leading and trailing white spaces are removed in the end.