Eiolon Posted November 24, 2010 Share Posted November 24, 2010 My script forces all names to start with a capital and lower case for the rest. However, some names have a ' or - in them. For example: Rosie O'Donnell or Carrie-Anne Moss With the script, the "D" in O'Donnell is lower case and the "A" in Anne is lower case. What do I need to do to make them capital? Here is what I am using: <?php echo ucwords(strtolower($row_persons['last_name'])) ?>, <?php echo ucwords(strtolower($row_persons['first_name'])) ?> Quote Link to comment https://forums.phpfreaks.com/topic/219705-capital-letters-after-apostrophe-or-hypen/ Share on other sites More sharing options...
jim_keller Posted November 24, 2010 Share Posted November 24, 2010 You may need a regexp for this. Try the code below. It's sort of a complicated regexp that captures the position of the lowercase letter appearing after an apostrophe or a dash, and I haven't tested it, but give it a whirl: $name = ucwords(strtolower($row_persons['last_name'])) . ', ' . ucwords(strtolower($row_persons['first_name'])); preg_match_all('/[\'\-]([a-z])/', $name, $matches, PREG_OFFSET_CAPTURE); foreach( $matches as $match_data ) { $match_text = $match_data[0][0]; $match_pos = $match_data[1][1]; $name = substr_replace($name, strtoupper($name), $match_pos); } echo "Fixed name is: {$name}<br />\n"; Quote Link to comment https://forums.phpfreaks.com/topic/219705-capital-letters-after-apostrophe-or-hypen/#findComment-1139050 Share on other sites More sharing options...
jim_keller Posted November 24, 2010 Share Posted November 24, 2010 *note that I edited the above code after posting Quote Link to comment https://forums.phpfreaks.com/topic/219705-capital-letters-after-apostrophe-or-hypen/#findComment-1139057 Share on other sites More sharing options...
MatthewJ Posted November 24, 2010 Share Posted November 24, 2010 Close... $name = ucwords(strtolower("O'connor")) . ', ' . ucwords(strtolower("Matthew")); preg_match_all('/[\'\-]([a-z])/', $name, $matches, PREG_OFFSET_CAPTURE); foreach( $matches as $match_data ) { $match_text = $match_data[0][0]; $match_pos = strpos($name, $match_text); $name = substr_replace($name, ucwords($match_text), $match_pos, 1); } echo "Match 1: $match_text <br />Match Pos: $match_pos<br />Fixed name is: {$name}<br />\n"; Quote Link to comment https://forums.phpfreaks.com/topic/219705-capital-letters-after-apostrophe-or-hypen/#findComment-1139063 Share on other sites More sharing options...
MatthewJ Posted November 24, 2010 Share Posted November 24, 2010 You could try this as well $name = ucwords(strtolower("O'connor")) . ', ' . ucwords(strtolower("Matthew")); if(strpos($name, "'")) { $pos = strpos($name, "'") + 1; } if(strpos($name, "-")) { $pos = strpos($name, "-") + 1; } $name = substr_replace($name, strtoupper($name[$pos]), $pos, 1); echo "Fixed name is: {$name}<br />\n"; Quote Link to comment https://forums.phpfreaks.com/topic/219705-capital-letters-after-apostrophe-or-hypen/#findComment-1139071 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.