Solved

fix string case

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mimic google as my ip 11 53
count download link and run update query 9 55
How can i modify my File download link ? 6 47
Hacked File Timestamps 4 49
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 count occurrences of each item in an array.

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now