removing extra trailing spaces in html field

7 posts by 2 authors in: Forums > CMS Builder
Last Post: September 24, 2010   (RSS)

By Deborah - September 22, 2010

In the display of a WYSIWYG field, I would like to replace any instances of two empty spaces or more following a period with a single space following the period.

Example:
Sentence 1. Sentence 2. Sentence 3. Sentence 4.

Becomes:
Sentence 1. Sentence 2. Sentence 3. Sentence 4.

This is the code I arrived at based on other examples, but it's not quite right, as the extra spaces still display.

<?php
$html = preg_replace("/\s\s+/", " ", $artistsRecord['bio'] );
echo $html; ?>

Any advice would be greatly appreciated.
~ Deborah

Re: [Deborah] removing extra trailing spaces in html field

By Chris - September 22, 2010

Hi Deborah,

I think you'll want to use this as your regular expression instead:

<?php
$html = preg_replace("/\.\s\s+/g", ". ", $artistsRecord['bio'] );
echo $html; ?>


The /g makes it "global", meaning that the replace will be run as many times as necessary on the string instead of just once.

I hope this helps. Please let me know if you have any questions.
All the best,
Chris

Re: [chris] removing extra trailing spaces in html field

By Deborah - September 23, 2010 - edited: September 23, 2010

Hi, Chris.

Thanks for your help.

That bit of code produced this error:
Warning: preg_replace(): Unknown modifier 'g' in ..........

I then escaped the backslash and the error doesn't display, but then there is no effect on the spaces.
<?php $html = preg_replace("/\.\s\s+\/g/", ". ", $artistsRecord['bio'] ); echo $html; ?>

~ Deborah

Re: [Deborah] removing extra trailing spaces in html field

By Chris - September 23, 2010

Hi Deborah,

I think I've been writing too much Perl recently! preg_replace does a global replace by default and there's no /g modifier.

I tried the first code that you posted out and it seems to work fine:

<?php $artistsRecord = array('bio' => "Sentence 1. Sentence 2. Sentence 3. Sentence 4. ") ?>
<pre>
<?php
$html = preg_replace("/\s\s+/", " ", $artistsRecord['bio'] );
echo $html; ?>
</pre>


Can you provide some example content that doesn't work with that preg_replace?
All the best,
Chris

Re: [Deborah] removing extra trailing spaces in html field

By Chris - September 24, 2010

Hi Deborah,

I think I've figured this one out. The WYSIWYG adds special non-breaking space characters (A0 in ASCII, which is C2A0 in UTF-8) and "\s" doesn't match them. Try this:

<?php
$html = preg_replace("/\.(\s|\xC2\xA0)+/", ". ", $artistsRecord['bio'] );
echo $html; ?>


That seems to work for me.

I hope this helps! Please let me know if you have any questions.
All the best,
Chris

Re: [chris] removing extra trailing spaces in html field

By Deborah - September 24, 2010

Chris,

You did figure it out! The new code works for me, too.

You may have solved another mystery for me at the same time. I was seeing some odd characters in auto-generated emails that were receiving text from a WYSIWYG field. Now I understand why.

Thanks for the brilliant detective work! This is a useful snippet of code for me!

~ Deborah