CHECKING FOR SPACES IN OTHERWISE BLANK FIELDS WHEN USING IF STATEMENTS - Mar 7th, 2023


When using “if” statements to suppress titles and other information in a viewer if a field is empty, it’s easy for
client’s to leave blank spaces in an otherwise empty field and defeat the “if” test.

Now, thanks to a plugin (CMSB version 2.04 or above required) written by Dave Edis of Interactive Tools, it’s even
easier to guard against this by checking to see if the content is text and not just a space and automatically deleting
the space before saving the record so that they can’t interfere with any “if” tests. It works in both text and
WYSIWYG fields

You can download this plugin from:

http://www.thecmsbcookbook.com/downloads/removeExtraWhitespace3.zip

The plugins won’t delete errant spaces in otherwise blank fields that already exist in your saved records, but you can
still check for them in each if statement used on your page.

To a normal “if” statement:



<?php if ($yourRecord['title']): ?>
Title: <?php echo $yourRecord['title'] ?>
<?php endif ?>



Just add this code:



&& !ctype_space($yourRecord['title'])



And the result:



<?php if ($record['title'] && !ctype_space($record['title'])): ?>
<?php echo $record['title'] ?>
<?php endif ?>



(Which means "if title has a value AND it's NOT (!) all spacing characters, then show it" will solve the problem.)

According to Dave Edis, This technique will not work if the content has PHP tags in it, but will for whitespace.

Also note this will not strip leading or trailing spaces from the content. To do that gets a little more complex.



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service