Solved

fix string case

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

747 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

12 Experts available now in Live!

Get 1:1 Help Now