Convert to Uppercase...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
revbackup
Forum Commoner
Posts: 29
Joined: Tue Jun 09, 2009 1:52 am

Convert to Uppercase...

Post 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...
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Convert to Uppercase...

Post 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;
 
Paul Arnold
Forum Contributor
Posts: 141
Joined: Fri Jun 13, 2008 10:09 am
Location: Newcastle Upon Tyne

Re: Convert to Uppercase...

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Convert to Uppercase...

Post 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
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply