Jump to content

[SOLVED] Manipulate String


robert_gsfame

Recommended Posts

hello codemasters...

 

i would like to ask some simple question, how can i change every first letter into capital letter

 

for example :      <?php $name = 'JIM HUMPHREY';?>

 

and i would like to make it into this when i call $name using echo

 

                                Jim Humphrey

 

 

and how bout if they have 3 words separated with space

 

for example :        $name = JIM HUMPHREY GODFREY

 

and it should result    Jim Humphrey Godfrey

 

 

thanks in advance

Link to comment
Share on other sites

No it doesn't work if all letters are capital letter..

 

if $name = JAMES HUGHES GODFREY then if i use ucwords then it will result the

 

same unless i have created $name = james hughes godfrey

 

So does it mean i have to create $name2 = strtolower($name) then ucwords($name2)????

 

it will need more effort, or do you have any better idea?

Link to comment
Share on other sites

ucwords(strtolower($name))

 

I prefer to use:

 

function titlecase($name)
{	return mb_convert_case(trim($name),MB_CASE_TITLE);
}

 

In order to use it you MAY have to define the character set first:

 

$charset = "utf-8";
mb_internal_encoding ($charset);

 

I have similar functions for uppercase and lowercase.

With the local functions you don't have to remember the MB_CASE_.... constants.

Link to comment
Share on other sites

any other ideas?

 

i have this problem with strtolower(ucwords

 

what if the strings that i want to manipulate is job title for example : $name = FINANCE MANAGER (FM)

 

if i use echo strtolower(ucwords($name))

 

then it comes Finance Manager (fm)

 

it should be --------------> Finance Manager (FM)

 

any other ideas?

 

 

 

 

 

Link to comment
Share on other sites

any other ideas?

 

i have this problem with strtolower(ucwords

 

what if the strings that i want to manipulate is job title for example : $name = FINANCE MANAGER (FM)

 

if i use echo strtolower(ucwords($name))

 

then it comes Finance Manager (fm)

 

it should be --------------> Finance Manager (FM)

 

any other ideas?

Will the things that you want non-effected be within ( )? If so you can do this:

<?php
$name = 'FINANCE MANAGER (FM)';
$split = explode(' ', $name);
foreach($split as &$part)
{
$part = ($part{0} != '(') ? ucfirst(strtolower($part)) : $part;
}
$name = implode(' ', $split);
echo $name;

Output: Finance Manager (FM)

Link to comment
Share on other sites

Here is what I came up with:

 

<?php
$str = ucwords(strtolower('FINANCE MANAGER (FM)'));
if(preg_match("~\((.+?)\)~i",$str,$matches)){
	$match = strtoupper($matches[0]);
	$str = preg_replace("~\((.+?)\)~i", $match, $str);
}
echo $str;
?>

 

Alternatively, you could do away with the need to create $match by integrating the $matches[1] right into an str_replace instead:

 

$str = ucwords(strtolower('FINANCE MANAGER (FM)'));
$str = (preg_match('#\(([^)]+)\)#', $str, $matches))? str_replace($matches[1], strtoupper($matches[1]), $str): $str;
echo $str;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.