MORE TRICKS WITH REGULAR EXPRESSIONS - Mar 12th, 2015


1) How to replace double quotes with 2 single quotes in a text field. (The field was a dimension field and the common
symbol for inches is a double quote.)

If your code doesn't like double quotes in the contents of a field, here's how to replace them with a set of 2 single
quotes that look pretty much the same.



<?php $your_table['your_field'] = preg_replace("[\"]", "''", $your_table['your_field']); ?>

Or for a variable


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


Then you can echo your field as you normally would and any double quotes will be replaced.

2) I needed to populate both a navigation menu entry and a page header from the contents of a text field called
member_meetings. The field contents was: “Monthly Artists&nbsp;&nbsp;Sharing Meeting”

The navigation menu required the Artists&nbsp;&nbsp; for the text to line up appropriately, but the header required:

A &nbsp; R &nbsp; T &nbsp; I &nbsp;S &nbsp; T &nbsp; S &nbsp; &nbsp; S &nbsp; H &nbsp; A &nbsp; R &nbsp; I &nbsp; N
&nbsp; G &nbsp; &nbsp;M &nbsp; E &nbsp; E &nbsp; T &nbsp; I &nbsp; N &nbsp; G



I knew that regular expressions could replace the characters for me as required, but couldn’t get the syntax to work
until Dave Edis from interactive Tools came to the rescue.

He offered the following solution:


<div align="center">

<?php $text = strtoupper($common_informationRecord['member_meetings']) ;
print
expandText($text);
// outputs: M &nbsp; O &nbsp; N &nbsp; T &nbsp; H &nbsp; L &nbsp; Y &nbsp; &nbsp;
// ...: A &nbsp; R &nbsp; T &nbsp; I &nbsp; S &nbsp; T &nbsp; S &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
// ...: S &nbsp; H &nbsp; A &nbsp; R &nbsp; I &nbsp; N &nbsp; G

//
function expandText($text) {
$text = preg_replace("/&nbsp;/i", '', $text); // replace &nbsp; with nothing
$text = preg_replace("|<br\s*/?>|i", "\n", $text); // replace breaks with nextlines
$text = preg_replace("/ /", ' ', $text); // 5 spaces between works (or replace " " with " ");
$text = preg_replace("/(\w)\B/", '\1 ', $text); // 3 spaces between letters (or replace "**" with "* *")
$text = preg_replace("/ /", ' &nbsp;', $text); // replace " " with " &nbsp;"
$text = preg_replace("/\n/i", "\n", $text); // replace nextlines with breaks
return $text;
}
?></div>


Capitalizing Hyphenated Names using str_replace and ucwords

To capitalize both halves of a hyphenated $last_name variable, you can use the following:


<?php function ucwordsHyphen($last_name){
return
str_replace('- ','-',ucwords(str_replace('-','- ',$last_name)));
}
?>
<?php $last_name = ucwordsHyphen(strtolower($record['last_name'])) ; ?>


Then where you want to display the last name:


<?php echo $last_name ?>





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