Solved

word case format

Posted on 2009-05-09
4
210 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 500 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

838 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