Help With Regular Expression

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

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  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.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Help With Regular Expression

By gkornbluth - March 7, 2012

Thank you Dave,

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
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

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