Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

word case format

Posted on 2009-05-09
4
Medium Priority
?
218 Views
Last Modified: 2012-05-06
hello there,
I have a php string and I would like to correct the format of some words..
$string='this pc is working slow because the pdf file is too big.. i should try and open it with my imac';
 
should be in this format
 
"this PC is working slow because the PDF file is too big.. i should try and open it with my iMac"
 
words changed PC, PDF, iMac

Open in new window

0
Comment
Question by:XK8ER
  • 3
4 Comments
 
LVL 16

Expert Comment

by:jessc7
ID: 24344983
If you know the list of words you would like to replace, you could look at using the str_ireplace function:

http://us2.php.net/manual/en/function.str-ireplace.php
In your example, using the following should work:



$string='this pc is working slow because the pdf file is too big.. i should try and open it with my imac';
 
$search  = array('pc', 'pdf', 'imac');
$replace = array('PC', 'PDF', 'iMac');
$newString = str_ireplace($search, replace , $string);

Open in new window

0
 
LVL 16

Expert Comment

by:jessc7
ID: 24344987
One minor syntax change to the above:
$string='this pc is working slow because the pdf file is too big.. i should try and open it with my imac';
 
$search  = array('pc', 'pdf', 'imac');
$replace = array('PC', 'PDF', 'iMac');
$newString = str_ireplace($search, $replace , $string);

Open in new window

0
 
LVL 7

Accepted Solution

by:
ycTIN earned 2000 total points
ID: 24346483
just use str_ireplace isn't enough.

<?php
$string='this pc is working slow because the pdf file is too big.. i should try and open it with my imac';
 
echo formatCase($string);
 
function formatCase($str) {
	$specialwords = array('PC', 'PDF', '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
 
LVL 16

Expert Comment

by:jessc7
ID: 24346508
I think ycTIN is headed down a more reusable path, but I think it can be made more simpler. Since str_ireplace is case insensitive, you wouldn't need two arrays in my previous example. You also wouldn't need to search for alternative case in strings manually. Here's a simplified example that works for me:
$string='this pc is working slow because the pdf file is too big.. i should try and open it with my imac';
 
$words = array('PC', 'PDF', 'iMac');
$newString = str_ireplace($words, $words , $string);
 
echo $newString;

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 …
Suggested Courses

886 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