Page 1 of 1

Convert to Uppercase...

Posted: Mon Jun 15, 2009 2:37 am
by revbackup
HI guys!

If I have a variable like this:

$text = 'The template $template is not available';

and I want a string function that would convert words that starts with $ into uppercase so that
the variable would contain like this:

$text = 'The template $TEMPLATE is not available';

How can I make it??? thanks...

Re: Convert to Uppercase...

Posted: Mon Jun 15, 2009 8:02 am
by Mark Baker

Code: Select all

 
$text = 'The template $template is not available';
$wordArray = str_word_count($text ,2,'$');
foreach ($wordArray as $key => $value) {
   if ($value{0} == '$') { $wordArray[$key] = strtoupper($value); }
}
$text = implode(' ',$wordArray);
echo $text;
 

Re: Convert to Uppercase...

Posted: Mon Jun 15, 2009 9:47 am
by Paul Arnold
Just as an aside, if your bulding a templating system, variables tend to be set as {VARIABLE} in the ones I've used in the past.
Using the $ sign might cause some confusing behaviour if combined with other variables that are not part of your templates.
Just something for you to consider.

Re: Convert to Uppercase...

Posted: Mon Jun 15, 2009 9:48 am
by pickle
Untested:

Code: Select all

$text = 'The template $template is not available';
$newText = preg_replace('/\$(.*) /',strtoupper('$1'),$text);
See example #4 on the preg_replace() doc page: http://ca2.php.net/manual/en/function.preg-replace.php