joshblue Posted March 12, 2009 Share Posted March 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/149079-solved-output-them-into-proper-cases/ Share on other sites More sharing options...
Mikedean Posted March 12, 2009 Share Posted March 12, 2009 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 ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/149079-solved-output-them-into-proper-cases/#findComment-782812 Share on other sites More sharing options...
joshblue Posted March 12, 2009 Author Share Posted March 12, 2009 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/149079-solved-output-them-into-proper-cases/#findComment-782822 Share on other sites More sharing options...
sasa Posted March 12, 2009 Share Posted March 12, 2009 try <?php $test = 'A*B*C COMPANY'; echo $proper = preg_replace_callback('/\*([a-z])/', create_function('$matches','return strtoupper($matches[1]);') , ucwords(strtolower($test))); ?> Quote Link to comment https://forums.phpfreaks.com/topic/149079-solved-output-them-into-proper-cases/#findComment-782844 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.