Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

get_content - remove all additional white space WordPress



I am trying to remove all multiple white spaces from get_content();

But in my html post editor I just have some extra white spaces, and there are no <strong>&nbsp</strong> characters, but in the output of the <strong>get_the_content</strong>, there additional &nbsp replacing the additional white space.

So I tried this...

$termsConditions = str_replace('&nbsp'," ",get_the_content());
echo preg_replace('/\s+/',' ',$termsConditions);

But still there are multiple &nbsp are in my text.

How are these being generated? or why is my php above not working?

Can anyone help please.


Answers (3)


Francisco Javier Carazo Gil answers:

With this you can clean every HTML special character: $Content = preg_replace("/&#?[a-z0-9]{2,8};/i","",$Content);

Francisco Javier Carazo Gil comments:

And this I think is better for you:
$string = htmlentities($string, null, 'utf-8');
$string = str_replace("&nbsp;", "", $string);

Josh Cranwell comments:

Thanks dude, the second bit worked.

Nice one.

Francisco Javier Carazo Gil comments:



daas answers:

$content = preg_replace( '/\s+/', ' ', get_the_content() );
echo $content;

It should work :)

or use this if it is non-breaking space

$content = str_replace('&nbsp;'," ",get_the_content());
$content = preg_replace( '/\s+/', ' ', $content );
echo $content;

Josh Cranwell comments:

Thanks for your answer tho the answer above worked for me. Thanks


Kiet Luong answers:

Let me help you do this !