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
Solved

fix string case

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

840 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