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...
Convert to Uppercase...
Moderator: General Moderators
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: Convert to Uppercase...
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...
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.
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...
Untested:
See example #4 on the preg_replace() doc page: http://ca2.php.net/manual/en/function.preg-replace.php
Code: Select all
$text = 'The template $template is not available';
$newText = preg_replace('/\$(.*) /',strtoupper('$1'),$text);Real programmers don't comment their code. If it was hard to write, it should be hard to understand.