473,786 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Last word of string UpperCase

3 New Member
Hello all, I try to accomplish the following.
I have a string like: “volkswage n-golf-gti” **
And I want it to change the string into:
“Volkswage n 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
2 4473
rienh
3 New Member
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 Recognized Expert Top Contributor
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
7067
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 does not.
18
7961
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 the string directly, I convert the string to a char using .ToCharArray() and set the first letter of each word to uppercase. The problem then is I can't figure out how to convert the char array back to a string. Whether I use .ToString() from the...
25
31799
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 = "one"
3
3511
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
1491
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
2473
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
1770
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 cafe\restaurant, the \hotel should be omitted or deleted..i'm using vb.net and here's my code if u could fix it... For Each arr As String In x.Split ( "\",3,StringSplitOptions.RemoveEmptyEntries) Me.txtdepartment.Text = arr ...
3
22132
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
16269
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 use a combobox based on a query that is based on the Employees table that allows me to sort on the last name. Currently the Names field consists of first_name second-fourth_names last_name eg Jane Elizabeth Francis Jones John Doe Abdullah...
3
2788
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 different each time. I looked at http://be.php.net/manual/en/book.strings.php but I either did not see it or I am looking at the wrong place (or both) houghi --
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.