Convert String to Proper Case


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function strProper($str) {
  2. $noUp = array('a','an','of','the','are','at','in');
  3. $str = trim($str);
  4. $str = strtoupper($str[0]) . strtolower(substr($str, 1));
  5. for($i=1; $i<strlen($str)-1; ++$i) {
  6. if($str[$i]==' ') {
  7. for($j=$i+1; $j<strlen($str) && $str[$j]!=' '; ++$j); //find next space
  8. $size = $j-$i-1;
  9. $shortWord = false;
  10. if($size<=3) {
  11. $theWord = substr($str,$i+1,$size);
  12. for($j=0; $j<count($noUp) && !$shortWord; ++$j)
  13. if($theWord==$noUp[$j])
  14. $shortWord = true;
  15. }
  16. if( !$shortWord )
  17. $str = substr($str, 0, $i+1) . strtoupper($str[$i+1]) . substr($str, $i+2);
  18. }
  19. $i+=$size;
  20. }
  21. return $str;
  22. }
  23. $str = "blah blah";
  24. echo strProper($str);

URL: http://us2.php.net/manual/en/function.strtoupper.php

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.