removing extra trailing spaces in html field
7 posts by 2 authors in: Forums > CMS Builder
Last Post: September 24, 2010 (RSS)
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
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.
Chris
Re: [chris] removing extra trailing spaces in html field
By Deborah - September 23, 2010 - edited: September 23, 2010
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
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?
Chris
Re: [chris] removing extra trailing spaces in html field
Here's an example of WYSIWYG text that is not affected by my original code:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis ligula lorem, consequat eget, tristique nec, auctor quis, purus. Vivamus ut sem. Fusce aliquam nunc vitae purus. Aenean viverra malesuada libero. Fusce ac quam.
If all spaces are stripped out, then replaced with a single space, that is what I'm trying to accomplish.
Thanks for your help.
~ Deborah
Re: [Deborah] removing extra trailing spaces in html field
By Chris - September 24, 2010
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.
Chris
Re: [chris] removing extra trailing spaces in html field
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