PHP 8.4.24 Released!

Voting

: six minus two?
(Example: nine)

The Note You're Voting On

Lech
10 years ago
This will correct capitalisation in names taking note of special capitalisation for Mc..., Mac..., and O'... Other special cases, of which I am not aware, can be added easily.

This is just a slight improvement on "deepdene at email dot com"'s name_case function... Thank you for original function.

<?php

function name_case($name) {
    if( !$name ) return $name;
    $newname = strtoupper($name[0]);
    $break = false;
    for( $i=1; $i<strlen($name); ++$i ) {
      $subed = substr($name, $i, 1);
      if( ord($subed) > 64 && ord($subed) < 123 || ord($subed) > 48 && ord($subed) < 58 ) {
        if( $break ) {
          $newname .= strtoupper($subed);
        }
        elseif( $i > 1 && in_array(substr($newname, $i-2, 2), array('Mc', 'O\'')) || $i > 2 && in_array(substr($newname, $i-3, 3), array('Mac')) ) {
          $newname .= strtoupper($subed); 
        }
        else {
          $newname .= strtolower($subed);
        }
        $break = false;
      }
      else {
        // not a letter - a boundary
        $newname .= $subed;
        $break = true;
      }
    }   
    return $newname;

?>

<< Back to user notes page

To Top