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 Sharing Meeting”
The navigation menu required the Artists for the text to line up appropriately, but the header required:
A R T I S T S S H A R I N G M E E T I N 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 O N T H L Y // ...: A R T I S T S // ...: S H A R I N G
// function expandText($text) { $text = preg_replace("/ /i", '', $text); // replace 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("/ /", ' ', $text); // replace " " with " " $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.