Solved

fix string case

Posted on 2009-05-12
3
157 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

724 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