62

How to remove   (that are hidden) and SPACES in below text but

  • hold UNICODE characters
  • hold <br> tag

i tested:

  • i used trim($string) => NOT WORKED
  • i used str_replace('&nbsp;', '', $string) => NOT WORKED
  • i used some regex => NOT WORKED

                <br>تاريخ ورود: یکشنبه ۲۳ بهمن ماه ۱۳۹۰
    

UPDATE: Image of hidden   Thanks

5
  • Use \s in your regex to match a space
    – Ben Carey
    Mar 26, 2012 at 11:09
  • i did it, but just spaces between words changed. &nbsp; are exist yet
    – Behnam
    Mar 26, 2012 at 11:10
  • Sorry, can you post the html up as text so I can copy it
    – Ben Carey
    Mar 26, 2012 at 11:30
  • Nothing on this entire page works for me...
    – igneosaur
    Apr 22, 2014 at 15:16
  • Final solution in above question worked for me. i.e. $string = htmlentities($string, null, 'utf-8'); $string = str_replace("&nbsp;", "", $string); Jan 27, 2017 at 12:27

4 Answers 4

91

This solution will work, I tested it:

$string = htmlentities($content, null, 'utf-8');
$content = str_replace("&nbsp;", "", $string);
$content = html_entity_decode($content);
1
  • 4
    O. My. God. Finally the solution, after hours of searching!! Thank you so much. I had issues with &nbsp; preventing my tinymce text to break nicely, so I replaced all of them with a real space: function b09_remove_forced_spaces($content) { $string = htmlentities($content, null, 'utf-8'); $content = str_replace("&nbsp;", " ", $string); $content = html_entity_decode($content); return $content; } add_filter("the_content", "b09_remove_forced_spaces", 9); (bad thing that stackoverflow doesn't allow for code blocks in comments
    – rassoh
    Jan 27, 2015 at 16:15
39

Not tested, but if you use something like:

$string = preg_replace("/\s/",'',$string);

That should remove all spaces.

UPDATE

To remove all spaces and &nbsp; references, use something like:

$string = preg_replace("/\s|&nbsp;/",'',$string);

UPDATE 2

Try this:

$string = html_entity_decode($string);

$string = preg_replace("/\s/",'',$string);

echo $string;

Forgot to say, reconvert the html entities so add this after the replacement:

htmlentities($string);
8
  • Okay, I'll see if I can test it myself
    – Ben Carey
    Mar 26, 2012 at 11:15
  • I am unable to test it but try preg_replace("/(\s)|(\&nbsp\;)/",'',$string);
    – Ben Carey
    Mar 26, 2012 at 11:17
  • Okay, I am looking into it further
    – Ben Carey
    Mar 26, 2012 at 11:20
  • Can you post up some of the html to your question, and then I can work with that
    – Ben Carey
    Mar 26, 2012 at 11:27
  • 2
    A different solution from @BalusC worked for me.
    – Tapper
    May 30, 2012 at 19:07
5

All solutions above kind of work, until one starts to work with German language where there are such letters:

ä &auml;

and othere simial ones. I use the following code:

$string = preg_replace ( "!\s++!u", ' ', $string );

More details here: PCRE(3) Library Functions Manual

1
  • A quantifier at the end of a pattern becomes de facto possessive: since it isn't followed by a subpattern, backtracking will never occur on it. So !\s+!u is shorter, as efficient and does the same. Jun 21 at 5:35
3

This worked for me.

preg_replace("/&nbsp;/",'',$string)

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.