using regex to uppercase certain words within strings ...
Moderator: General Moderators
-
Gordon Werner
- Forum Newbie
- Posts: 3
- Joined: Tue Apr 03, 2007 4:14 pm
using regex to uppercase certain words within strings ...
Good afternoon.
I apologize if this has been asked before but I am not sure what to search for.
I am parsing files uploaded by users that sometimes type in UPPERCASE ONLY. I can change the case of words with strtolower, ucwords, etc ... but how do I do the following?
Lets say that I ucwords a string "14 SSW OMAHA" ... this would then become "14 Ssw Omaha".
What do I need to do to make the "Ssw" into "SSW"
the possibilities are:
Nw
Ne
Sw
Se
Nnw
Nne
Ssw
Sse
Any help would be appreciated.
Thanks
G.
I apologize if this has been asked before but I am not sure what to search for.
I am parsing files uploaded by users that sometimes type in UPPERCASE ONLY. I can change the case of words with strtolower, ucwords, etc ... but how do I do the following?
Lets say that I ucwords a string "14 SSW OMAHA" ... this would then become "14 Ssw Omaha".
What do I need to do to make the "Ssw" into "SSW"
the possibilities are:
Nw
Ne
Sw
Se
Nnw
Nne
Ssw
Sse
Any help would be appreciated.
Thanks
G.
-
Gordon Werner
- Forum Newbie
- Posts: 3
- Joined: Tue Apr 03, 2007 4:14 pm
well ...
I can change them all individually
$location = ereg_replace('Ssw' , 'SSW ', $location);
$location = ereg_replace('Sse' , 'SSE ', $location);
etc ...
but I would like to know if it is possible to have one statements that will change them all regardless of which compass point they supply in their text.
I can change them all individually
$location = ereg_replace('Ssw' , 'SSW ', $location);
$location = ereg_replace('Sse' , 'SSE ', $location);
etc ...
but I would like to know if it is possible to have one statements that will change them all regardless of which compass point they supply in their text.
-
Gordon Werner
- Forum Newbie
- Posts: 3
- Joined: Tue Apr 03, 2007 4:14 pm
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
Code: Select all
$text = "14 Ssw Omaha";
echo preg_replace_callback('#[nNsS][nNsS]?[we]#',create_function('$m','return strtoupper($m[0]);'),$text);- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
quite right - my mistake!
Thanks feyd 
Code: Select all
#\b[nNsS][nNsS]?[we]\b#