MATCHING A PATERN OR REPLACING ONE CHARACTER WITH ANOTHER USING “REGULAR EXPRESSIONS” - Jun 3rd, 2017


REPLACING CHARACTERS
Sometimes you can run into problems when there’s a space, a dash or underscore between words in a field value. I ran
into this recently with an implementation of Lightbox.

My client wanted to use a 2 word value for a field and Lightbox wanted to see a single word with no spaces. If I
wasn’t displaying this value in other places on the page I could have put a dash or underscore in the value instead of
a space.

I chose to use a dash to make Lightbox happy and to replace the dash with a space where it appeared on the page.

Here’s how for a multi record editor:(thanks to Chris Waddell at Interactive Tools for the code syntax)

Just insert the line below into your code.



<?php foreach ($your_tableRecords as $record): ?>

<!-- Insert This Code -->

<?PHP $record['your_field'] = preg_replace("/[-]/", "&nbsp;", $record['your_field'] ); ?>

<!-- End of Insert-->

<?php echo $record['your_field] ?>

<?php endforeach; ?>



Chris explained:

/[-]/ means "match a single character in the set [-]".
preg_replace() will repeat the match as many times as it can.

It’s also possible to match more than one character in the set by adding the additional characters as shown below.
Changing the code above to this would replace any occurrence of a dash, an underscore, an r or an x with a space. The
replacement will be case sensitive.



<?PHP $record['your_field'] = preg_replace("/[-_rx]/”, "&nbsp;", $record['your_field'] ); ?>



To make the replacement case insensitive, just ad an “i” to the end of the expression, like this:



<?PHP $record['your_field'] = preg_replace("/[-_rx]/i”, "&nbsp;", $record['your_field'] ); ?>



If you wanted to replace 2 different characters with 2 different ones, you’d do it by adding a second line of
“preg_replace” code like this:



<?PHP $record['your_field'] = preg_replace("/[-]/", "&nbsp;", $record['your_field'] ); ?>
<?PHP $record['your_field'] = preg_replace("/[_]/", "*", $record['your_field'] ); ?>



The above code will replace any occurrence of a dash ("-") in the string with a space (" "). Then it will replace any
occurrence of an underscore ("_") with an asterisk ("*").

For a single record editor, the code syntax would be.



<?PHP $your_tableRecord['your_field'] = preg_replace("/[-_rx]/”, "&nbsp;", $your_tableRecord['your_field'] ); ?>



If you want to replace a complete expression like rx with another character then remove the forward slashes around the
expression

So This:



<?PHP $your_tableRecord['your_field'] = preg_replace("/[rx]/”, "&nbsp;", $your_tableRecord['your_field'] ); ?>



Would become this:



<?PHP $your_tableRecord['your_field'] = preg_replace("[rx]”, "&nbsp;", $your_tableRecord['your_field'] ); ?>



When using Lightbox and other utilities, an extra double quote can cause the code to become broken. If you are using an
uploads info fields you can't specify characters as allowed, so I use this little trick to replace any inadvertent
double quotes with 2 single quotes, which looks (almost) the same.



<?PHP $upload['info1'] = preg_replace("[\"]", "''", $upload['info1'] ); ?>



In order to replace backslashes they must be escaped. Here's how to replace all backslashes with nothing:


<?PHP $your_tableRecord['your_field'] = preg_replace("[\\\\]”, "", $your_tableRecord['your_field'] ); ?>



or


<?PHP $your_variable = preg_replace("[\\\\], "", $your_variable ); ?>


According to Dave Edis, there are many other uses for “regular expressions”. He commented: “Basically regular
expressions are ways of describing patterns. They're used to matching, and replacing content. So this is useful for
validating input (such as determining if an url, credit card number, phone number, or email is in the right format),
replacing content (stripping html tags, adding extra tags or attributes, etc, or parsing out data (matching all the
email addresses in a web page, parsing a CSV file and importing the data, or extracting some data (such as the current
temp from a weather web page to use somewhere else).

In PHP all the regular expression functions start with preg_ and in the CMS Builder code base we use those functions in
over 200 places. So they come in quite handy. If your web page editor software has a "Find in Files" or search
multiple files feature you can search the CMSB code for "preg_" and see some of the ways we use it.”

MATCHING A PATTERN

If you wanted anything other than between 11 to 14 characters that were either numbers or spaces in a phone number field
and throw an error message if the criteria was not met, you could use something like:

if(!preg_match("/^[ 0-9]{11,14}$/", @$phone )){
$errorsAndAlerts .= "Phone numbers must be between 10 and 14 characters long and can not contain anything but numbers
and spaces \n";}

You can see a list of the PHP functions here:

http://ca3.php.net/manual/en/ref.pcre.php

Here’s a link to a regex tutorial:

http://www.regular-expressions.info/tutorialcnt.html

Here’s a link to a regex cheat sheet:

http://www.bitcetera.com/page_attachments/0000/0030/regex_in_a_nutshell.pdf

And a few on-line regex testers/debuggers:

http://www.regexplanet.com/advanced/java/index.html

https://regex101.com/




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