Solved

fix string case

Posted on 2009-05-12
3
156 Views
Last Modified: 2012-05-06
hello there,
I have a function that converts strings into lowercase and if some keywords are found then it will fix them to its correct case..
I would like to implement something so that if if the strings like these are found

name test [big]
testing (small)
some text 200a
a-one

these are just sample so I would need to implement something so that in case of
[big] would be [Big] or
(small) would be (Small)
200a1 would be 200A1
a-one would be A-One

basically to make something so that the first letter is upper case after a char that is no [a-z]
function formatCase($str) {
$specialwords = array('FIFA','IV','VA','AV','DJ','CAD','RAR','ZIP','IDM','GTA','iMac');
 
	$directory = array();
	foreach($specialwords as $special) {
	        $directory[strtolower($special)] = $special;
	}
	$result = '';
	foreach (explode(' ',$str) as $word) {
	        $key = strtolower($word);
	        $result .= (isset($directory[$key]) ? $directory[$key] : $word).' ';
	}
	return trim($result);
}

Open in new window

0
Comment
Question by:XK8ER
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 7

Accepted Solution

by:
Jonah11 earned 500 total points
ID: 24370621
untested, but after line 9 insert:

not that this accomplishes "basically to make something so that the first letter is upper case after a char that is no [a-z]" but, in the case of a-one, would convert it to a-One, not A-One.  perhaps a special case is needed for that?  
$word = preg_replace_callback(
        '|[^a-z][a-z]|',
        create_function(
            // single quotes are essential here,
            // or alternative escape all $ as \$
            '$matches',
            'return strtoupper($matches[0]);'
        ),
        $word
    );

Open in new window

0
 
LVL 7

Expert Comment

by:Jonah11
ID: 24370626
note that in the above post "not that" should read "note that" :)
0
 
LVL 7

Expert Comment

by:Jonah11
ID: 24370632
also, in my code snippet you may want to change line 2 to read:
'|[^a-zA-Z][a-z]|',

Open in new window

0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question