473,378 Members | 1,236 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Last word of string UpperCase

3
Hello all, I try to accomplish the following.
I have a string like: “volkswagen-golf-gti” **
And I want it to change the string into:
“Volkswagen Golf GTI” (last part completely uppercase)

But I have some difficulties understanding preg_replace.
Right now I have the following code:

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. $string = 'volkswagen-golf-gti'; 
  3. $string2 = ucwords(str_replace('-', ' ', $string)); 
  4. $string2 = preg_replace("/\s+\S+$/e", "strtoupper('\\1')", $string2); 
  5. echo $string2; 
  6. ?>
Without line 4 enabled, the result of the echo $string2 is:
Volkswagen Golf Gti (is wrong, last part is not completely uppercase)

With line 4 enabled, the last part of the $string is removed
so echo $string2 gives me :
Volkswagen Golf
How can I change the code so it select the last part of $string2 and makes that part uppercase? So the result of echo $string2; will be:
Volkswagen Golf GTI

** $string can be anything, not only volkswagen-golf-gti
Aug 30 '10 #1

✓ answered by rienh

Thanks to the solution of NoDog for providing me the solution, and solving my problem. The following code, did do the trick for me.

Expand|Select|Wrap|Line Numbers
  1. $var = 'volkswagen-golf-gti';
  2. $regexp = array(
  3.    '/-/' => ' ',
  4.    '/.*/e' => 'ucwords("$0")',
  5.    '/\b\S+$/e' => 'strtoupper("$0")'
  6. );
  7. $var2 = preg_replace(array_keys($regexp), $regexp, $var);
  8. echo $var2;

2 4424
rienh
3
Thanks to the solution of NoDog for providing me the solution, and solving my problem. The following code, did do the trick for me.

Expand|Select|Wrap|Line Numbers
  1. $var = 'volkswagen-golf-gti';
  2. $regexp = array(
  3.    '/-/' => ' ',
  4.    '/.*/e' => 'ucwords("$0")',
  5.    '/\b\S+$/e' => 'strtoupper("$0")'
  6. );
  7. $var2 = preg_replace(array_keys($regexp), $regexp, $var);
  8. echo $var2;
Sep 1 '10 #2
kovik
1,044 Expert 1GB
This could have been done without regex. Assuming that the delimiter between your words is a space, you break the string apart, set the last word to uppercase, and glue the string back together. I just built a quick little function for it. No regex, just array manipulation.

Function:
Expand|Select|Wrap|Line Numbers
  1. function last_word_uppercase($sentence, $delimiter = ' ') {
  2.     $words = explode($delimiter, $sentence);
  3.     if (!empty($words)) {
  4.         $lastWord = array_pop($words);
  5.         $words[] = strtoupper($lastWord);
  6.         return implode($delimiter, $words);
  7.     }
  8.  
  9.     return strtoupper($sentence);
  10. }

Usage:
Expand|Select|Wrap|Line Numbers
  1. $str = 'This is a sentence.';
  2. echo last_word_uppercase($str);

Result:
Expand|Select|Wrap|Line Numbers
  1. This is a SENTENCE.
Sep 1 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Mason A. Clark | last post by:
LAST WORD(s): 1. MSIE6 and Firefox will go to the top of the page on command <a href="#top">go upsy</a> even if there is NO name="top" or id="top" They know what a "top" is :-) Opera...
18
by: Marcio Kleemann | last post by:
I need to force the first letter of each word in a line of text to uppercase. The text comes from a TextBox control in a Web Form. I'm new to ..NET and am having a problem. Since I can't modify...
25
by: electrixnow | last post by:
in MS VC++ Express I need to know how to get from one comma delimited text string to many strings. from this: main_string = "onE,Two,Three , fouR,five, six " to these: string1 =...
3
by: shapper | last post by:
Hello, How can I make the first letter of a string to be upper case and all the others lower case? Thanks, Miguel
6
by: Ilias Lazaridis | last post by:
- Ï/Ç metaperl: Ilias Lazaridis: Michael Bayer wrote within: http://groups.google.com/group/sqlalchemy/msg/9d7a096a61abfc6f I understand your elaborations.
4
by: titan nyquist | last post by:
Why does ToTitleCase not work if the case is already in upper case? I have to make my string lowercase, first, before I pass to to ToTitleCase to have it work. Titan
5
by: neobagsjol | last post by:
hi guys.. i have a problem on how to omit the last word..like for example i have this words; cafe\restaurant\hotel i would only like to view on the textbox.text the words only...
3
by: madhurchadha | last post by:
how do i remove the last word from a string in c# suppose string is "hi how are you" i wanna remove you
2
by: sfindlay | last post by:
Hi My Employees table contains staffs' full names as it is an import from the HR system and I don't have control over the format. Many of our staff have names 4, 5 or even 6 words long. I wish to...
3
by: houghi | last post by:
I have a parameter $name that contavins e.g. "Turn left at Blooming Grove Turnpike/Quassaic Turnpike/RT-94 Continue to follow RT-94". I just want to have the last word. The number of words will be...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.