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
https://forums.phpfreaks.com/topic/149079-solved-output-them-into-proper-cases/
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 );
?>

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!!!

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.