Pay money and get answers for your WordPress questions (more info)

get_content - remove all additional white space

  • SOLVED


Hello,

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...

<?php
$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.


Thanks



Answers (3)

2013-01-25

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:

Perfect!

2013-01-25

daas answers:


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


It should work :)

<strong>Edit</strong>
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

2013-01-25

Kiet Luong answers:

Let me help you do this !