Help With Regular Expression
5 posts by 2 authors in: Forums > CMS Builder
Last Post: March 7, 2012 (RSS)
By gkornbluth - March 7, 2012
I’m trying to produce this (3 spaces between characters, 5 between words):
A R T I S T S S H A R I N G M E E T I N G
From a text field (member_meetings) containing this: “Monthly Artists  Sharing”, using a regular expression and I can’t seem to get it right.
I read that \b and \B can be used to match the space between characters (boundaries) so here’s what I’ve tried (which didn’t work) and only rendered this: MONTHLY ARTISTS  SHARING
<?PHP $common_informationRecord['member_meetings'] = preg_replace("[\b\B ]i", " ", $common_informationRecord['member_meetings'] ); ?><?php echo strtoupper($common_informationRecord['member_meetings']) ?>
Any ideas would be great.
Thanks,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] Help With Regular Expression
By Dave - March 7, 2012
That's kind of tricky! It's likely possible with a single regular expression, but often easier just to break it down into multiple steps that are simpler and write a function to wrap them in. Try this:
$text = "MONTHLY ARTISTS&NBSP;&NBSP;SHARING";
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
exit;
//
function expandText($text) {
$text = preg_replace("/ /i", ' ', $text); // replace with spaces
$text = preg_replace("/ /", ' ', $text); // 5 spaces between words (or replace " " with " ");
$text = preg_replace("/(\w)\B/", '\1 ', $text); // 3 spaces between letters (or replace "**" with "* *")
$text = preg_replace("/ /", ' ', $text); // replace " " with " "
return $text;
}
Let me know if that works for you.
interactivetools.com
Re: [Dave] Help With Regular Expression
By gkornbluth - March 7, 2012
It worked perfectly.
I forgot one detail.
I need to be able to insert a<br /> <br> or <br > in the text.
IE: MONTHLY<br />ARTISTS&NBSP;&NBSP;SHARING
I can't seem to figure out how to retain it to create a carriage return in the rendered text.
Thanks again,
Jerry
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] Help With Regular Expression
By Dave - March 7, 2012
Tricky indeed, try this:
//
function expandText($text) {
$text = preg_replace("/ /i", ' ', $text); // replace with spaces
$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", "<br />\n", $text); // replace nextlines with breaks
return $text;
}
And a CSS only solution:
<span style="letter-spacing: 12px; word-spacing:12px;">
MONTHLY ARTISTS<br />&NBSP;&NBSP;SHARING
</span><br/>
Hope that helps!
interactivetools.com
Re: [Dave] Help With Regular Expression
By gkornbluth - March 7, 2012
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php