Jump to content

[SOLVED] Output them into Proper Cases


joshblue

Recommended Posts

 

I'm working with a text file with all of the data's their are in uppercase. Basically, I need to create a script that would output them into proper case..

 

So if ever I have this word:

ABC COMPANY

 

It must be output as:

ABC Company

 

But I know its hard to figure that out so theirs actually an identifier for them. The identifier would be an asterisk (*). So, if theirs an asterisk beside the letter, that letter will be turn to uppercase like this:

 

A*B*C COMPANY - theirs an asterisk beside the letters B and C so those letter must be in uppercase. The letters A in ABC and C in COMPANY is alright since their are first letters so its easy for them to convert using ucwords() function

 

So that word would be output as:

ABC Company

 

 

 

I hope you understand what I meant. Hope you somebody would give me an idea on this...

 

 

Thanks

Link to comment
Share on other sites

I'm sure you could do it in RegEx, but this is a way you can do it in PHP.

 

<?php
$string = "A*B*C COMPANY";

function strToProper( $theString )
{
	$differentWords = explode( " ", $theString );

	for( $i = 0; $i < count( $differentWords ); $i++ )
	{
		$thisWord = strtolower( $differentWords[$i] );
		$thisWord = strtoupper( $thisWord[0] ) . substr( $thisWord, 1 );
		$innerCapitals = explode( "*", $thisWord );
		if( count( $innerCapitals != 0 ) )
		{
			for( $n = 1; $n < count( $innerCapitals ); $n++ )
			{
				$innerCapitals[$n] = strtoupper( $innerCapitals[$n][0] ) . substr( $innerCapitals[$n], 1 );
			}
			$thisWord = implode( "", $innerCapitals );
		}
		$differentWords[$i] = $thisWord;
	}

	return implode( " ", $differentWords );
}

echo strToProper( $string );
?>

Link to comment
Share on other sites

I'm sure you could do it in RegEx, but this is a way you can do it in PHP.

 

<?php
$string = "A*B*C COMPANY";

function strToProper( $theString )
{
	$differentWords = explode( " ", $theString );

	for( $i = 0; $i < count( $differentWords ); $i++ )
	{
		$thisWord = strtolower( $differentWords[$i] );
		$thisWord = strtoupper( $thisWord[0] ) . substr( $thisWord, 1 );
		$innerCapitals = explode( "*", $thisWord );
		if( count( $innerCapitals != 0 ) )
		{
			for( $n = 1; $n < count( $innerCapitals ); $n++ )
			{
				$innerCapitals[$n] = strtoupper( $innerCapitals[$n][0] ) . substr( $innerCapitals[$n], 1 );
			}
			$thisWord = implode( "", $innerCapitals );
		}
		$differentWords[$i] = $thisWord;
	}

	return implode( " ", $differentWords );
}

echo strToProper( $string );
?>

 

 

Wow! Thank you very much!!!

 

 

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.