Capitalization using PHP

不想你离开。 提交于 2019-12-25 19:58:08

问题


I am having trouble with the capitalization of names using PHP. There are some names that have 2 capital letters in them (ex: McCall). When storing a users name that registers for our website, we run the following code:

$name = ucwords(strtolower(trim($_SESSION['last_name']))) ;

What this does is change 'mccall' to 'Mccall'. What we need is a way to check if the first 2 letters begin with 'Mc' and if so, the 3rd letter will be capitalized as well changing the name to 'McCall'.

Any help is greatly appreciated.


回答1:


$name = 'mccall';
$name = ucwords(strtolower(trim($name))) ;

if (strpos($name, 'Mc') === 0) {
    $name = 'Mc' . ucwords(substr($name, 2, strlen($name)));
}
echo $name; // McCall



回答2:


Here's a solution that will just catch Mc, if that's really all you're interested in:

function capitalizeLastName($name) {
    $result = ucwords(strtolower(trim($_SESSION['last_name'])));

    if(substr($result, 0, 2) === 'Mc') {
        $result[2] = strtoupper($result[2]); // Yes, this works in PHP!
    }

    return $result;
}



回答3:


Just FYI, I have a REALLY nice function for capitalizing a lot more than just names. It also does titles, phrases, etc...

It takes a string parameter and a boolean. The bool is TRUE if you're capitalizing a name, and FALSE or nothing if it's a title, phrase, etc...

Use is as simple as:

capitalize('mccall', TRUE); // result 'McCall'
capitalize('merry christmas and happy holidays!'); // result 'Merry Christmas and Happy Holidays!'
capitalize('jd mckinstry', TRUE); // result 'JD McKinstry'

The fun ction!

function capitalize($str, $is_name=FALSE) {
    $str = trim(stripcslashes($str));

    // exceptions to standard case conversion
    if ($is_name) {
        $all_uppercase = '';//'Aj|Jd';
        $all_lowercase = 'De La|De Las|Der|Van De|Van Der|Vit De|Von|Or|And|Van ';
    }
    else {  // addresses, essay titles ... and anything else
        $all_uppercase = 'Po|Rr|Se|Sw|Ne|Nw';
        $all_lowercase = 'A|And|As|By|In|Of|Or|To';
    }

    $prefixes = 'Mc';
    $suffixes = "'S";

    // captialize all first letters
    $str = preg_replace('/\\b(\\w)/e', 'strtoupper("$1")', strtolower(trim($str)));

    if ($all_uppercase) {   // capitalize acronymns and initialisms e.g. PHP
        $str = preg_replace("/\\b($all_uppercase)\\b/e", 'strtoupper("$1")', $str);
    }

    if ($is_name) {
        if (strpos($str, " ") !== FALSE) {
            $ara = explode(" ", $str);
            foreach ($ara as $k => $v) {
                if ($k != count($ara)-1 && !preg_match("/[aeiouyAEIOUY]/", $v)) $ara[$k] = strtoupper($v);
            }
            $str = implode(" ", $ara);
        }
        elseif (!preg_match("/[aeiouy]/", $str)) {
            $str = strtoupper($str);
        }
    }

    if ($all_lowercase) {   // decapitalize short words e.g. and
        if ($is_name) { // all occurences will be changed to lowercase
            $str = preg_replace("/\\b($all_lowercase)\\b/e", 'strtolower("$1")', $str);
        }
        else {  // first and last word will not be changed to lower case (i.e. titles)
            $str = preg_replace("/(?<=\\W)($all_lowercase)(?=\\W)/e", 'strtolower("$1")', $str);
        }
    }

    if ($prefixes) {    // capitalize letter after certain name prefixes e.g 'Mc'
        $str = preg_replace("/\\b($prefixes)(\\w)/e", '"$1".strtoupper("$2")', $str);
    }

    if ($suffixes) {    // decapitalize certain word suffixes e.g. 's
        $str = preg_replace("/(\\w)($suffixes)\\b/e", '"$1".strtolower("$2")', $str);
    }

    //  Mac Exceptions
    if (strpos($str, "Macd") === FALSE || strpos($str, "Macv") === FALSE) {
        $str = preg_replace("/Macd/", 'MacD', $str);
        $str = preg_replace("/Macv/", 'MacV', $str);
    }

    return trim(stripcslashes($str));
}

As some of you might notice, this was originally taken from the PHP site, but I've since nurtured it and matured it into a much more useful and "intelligent" method!




回答4:


I found this answer as I was searching on the function found in the PHP manual, as answerd by SpYk3HH.

The preg_replace has been deprecated as correctly noticed by Michelangelo. To save others some time here the renewed function.

Plus some additions to support Dutch names:

<?php

function capitalize($str, $is_name = FALSE) {

  $mylist = "S|‘s|‘t|A|Aan|Aan ‘t|Aan De|Aan Den|Aan Der|Aan Het|Aan T|Af|Al|Am|"
      . "Am De|Auf|Auf Dem|Auf Den|Auf Der|Auf Ter|Aus|Aus ‘m|Aus Dem|Aus Den|"
      . "Aus Der|Aus M|Ben|Bij|Bij ‘t|Bij De|Bij Den|Bij Het|Bij T|Bin|Boven D|"
      . "Boven D’|D|D’|Da|Dal|Dal’|Dalla|Das|De|De Die|De Die Le|De L|De L’|"
      . "De La|De Las|De Le|De Van Der|Deca|Degli|Dei|Del|Della|Den|Der|Des|"
      . "Di|Die Le|Do|Don|Dos|Du|El|Het|I|Im|In|In ‘t|In De|In Den|In Der|"
      . "In Het|In T|L|L’|La|Las|Le|Les|Lo|Los|Of|Onder|Onder ‘t|Onder De|"
      . "Onder Den|Onder Het|Onder T|Op|Op ‘t|Op De|Op Den|Op Der|Op Gen|Op Het|"
      . "Op T|Op Ten|Over|Over ‘t|Over De|Over Den|Over Het|Over T|S’|T|Te|Ten|"
      . "Ter|Tho|Thoe|Thor|To|Toe|Tot|Uijt|Uijt ‘t|Uijt De|Uijt Den|Uijt Te De|"
      . "Uijt Ten|Uit|Uit ‘t|Uit De|Uit Den|Uit Het|Uit T|Uit Te De|Uit Ten|"
      . "Unter|Van|Van ‘t|Van De|Van De L|Van De L’|Van Den|Van Der|Van Gen|"
      . "Van Het|Van La|Van T|Van Ter|Van Van De|Ver|Vom|Von|Von ‘t|Von Dem|"
      . "Von Den|Von Der|Von T|Voor|Voor ‘t|Voor De|Voor Den|Voor In ‘t|"
      . "Voor In T|Vor|Vor Der|Zu|Zum|Zur|Vit De|Or|And|Van|En";



  $str = trim($str);
  $str = stripcslashes($str);

  // exceptions to standard case conversion
  if ($is_name) {
    $all_uppercase = ''; //'Aj|Jd';
    $all_lowercase = $mylist;
  }
  else {  // addresses, essay titles ... and anything else
    $all_uppercase = 'Po|Rr|Se|Sw|Ne|Nw';
    $all_lowercase = 'A|And|As|By|In|Of|Or|To';
  }

  $prefixes = 'Mc';
  $suffixes = "'S";

  // captialize all first letters
  $str = preg_replace_callback('/\\b(\\w)/', function($m) {
    return strtoupper($m[1]);
  }, strtolower(trim($str)));

  if ($all_uppercase) {   // capitalize acronymns and initialisms e.g. PHP
    $str = preg_replace("/\\b($all_uppercase)\\b/", function($m) {
      return strtoupper($m[1]);
    }, $str);
  }

  if ($is_name) {
    if (strpos($str, " ") !== FALSE) {
      $ara = explode(" ", $str);
      foreach ($ara as $k => $v) {
        if ($k != count($ara) - 1 && !preg_match("/[aeiouyAEIOUY]/", $v))
          $ara[$k] = strtoupper($v);
      }
      $str = implode(" ", $ara);
    }
    elseif (!preg_match("/[aeiouy]/", $str)) {
      $str = strtoupper($str);
    }
  }

  if ($all_lowercase) {   // decapitalize short words e.g. and
    if ($is_name) { // all occurences will be changed to lowercase
      $str = preg_replace_callback("/\\b($all_lowercase)\\b/", function($m) {
        return strtolower($m[1]);
      }, $str);
    }
    else {  // first and last word will not be changed to lower case (i.e. titles)
      $str = preg_replace_callback("/(?<=\\W)($all_lowercase)(?=\\W)/", function($m) {
        return strtolower($m[1]);
      }, $str);
    }
  }

  if ($prefixes) {    // capitalize letter after certain name prefixes e.g 'Mc'
    $str = preg_replace_callback("/\\b($prefixes)(\\w)/", function($m) {
      return $m[1] . strtoupper($m[2]);
    }
        , $str);
  }

  if ($suffixes) {    // decapitalize certain word suffixes e.g. 's
    $str = preg_replace_callback("/(\\w)($suffixes)\\b/", function($m) {
      return $m[1] . strtoupper($m[2]);
    }, $str);
  }

  //  Mac Exceptions
  if (strpos($str, "Macd") === FALSE || strpos($str, "Macv") === FALSE) {
    //$str = preg_replace_callback("/Macd/", 'MacD', $str);
    //$str = preg_replace_callback("/Macv/", 'MacV', $str);
  }

  return trim(stripcslashes($str));
}

//Array containing names to test
$testnames = [
  'van der vaart',
  'van vollenhoven',
  "van 't zandt",
  'van het zand',
  'el hamdoie',
  'van der Rooi-van Velzen',
  'Zuidewijn - van rooien',
  'teggelen onder t boven',
  "guido op 't drooge",
  "friso van drooge",
  'Zuidewijn - van rooien',
  'teggelen onder t boven',
  'ZUID-HOLLAND',
  "'s hertogen-bosch",
  "De Rooi Van Zuidewijn",
  "van onder",
  "Van Der Wijk-Zeewuster",
  "de Vries-van der Leest",
  "Den Oudsten - van 't Veldt",
  "Hare Koninklijke Hoogheid Alexia Juliana Marcela Laurentien Prinses der Nederlanden, Prinses van Oranje-Nassau",
  "Hare Koninklijke Hoogheid Máxima, Prinses der Nederlanden, Prinses van Oranje-Nassau, Mevrouw van Amsberg",
  "van Lippe-Biesterfeld van Vollenhoven",
];

foreach ($testnames as $name) {

  //First convert name to uppercase to get rid of the correct caps
  $name = strtoupper($name);
  //Output the capitalized name
  echo $name;
  echo " = ";
  //See output of function
  echo capitalize($name, TRUE);
  //Set br tag to get the next test name on next line
  echo "<br />";
}


来源:https://stackoverflow.com/questions/9551840/capitalization-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!