Help With Regular Expression

5 posts by 2 authors in: Forums > CMS Builder
Last Post: March 7, 2012   (RSS)

By gkornbluth - March 7, 2012

Hi All,

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", " &nbsp; ", $common_informationRecord['member_meetings'] ); ?><?php echo strtoupper($common_informationRecord['member_meetings']) ?>

Any ideas would be great.

Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







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

Hi Jerry,

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 &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
exit;

//
function expandText($text) {
$text = preg_replace("/&nbsp;/i", ' ', $text); // replace &nbsp; 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("/ /", ' &nbsp;', $text); // replace " " with " &nbsp;"
return $text;
}


Let me know if that works for you.
Dave Edis - Senior Developer
interactivetools.com

Re: [gkornbluth] Help With Regular Expression

By Dave - March 7, 2012

Hey Jerry,

Tricky indeed, try this:
//
function expandText($text) {
$text = preg_replace("/&nbsp;/i", ' ', $text); // replace &nbsp; 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("/ /", ' &nbsp;', $text); // replace " " with " &nbsp;"
$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!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Help With Regular Expression

By gkornbluth - March 7, 2012

WOW!!!
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php