Formatting Human Names - PHP (or any language) library? -
is there way format human names? example, "john doe" should "john doe". or "angus macgyver" should "angus macgyver". etc.
i know solution not going complete (there's many rules names), better nothing. suggestions?
as suggested in comments, in php, can this:
$name_formatted = ucfirst(strtolower($name_unformatted));
this handle 90% of cases. put function , add rules deal macguyver, o'reilly type exceptions.
update: pointed out, ucfirst first word in string. use regex uppercase first letters in every word, or function this:
<?php $name_unformatted = "john doe"; function format_name($name_unformatted) { $name_formatted = ucwords(strtolower($name_unformatted)); // handle 90% of names // ucwords work strings, if wanted break out each word can deal exceptions, this: $separator = array(" ","-","+","'"); foreach($separator $s) { if (strpos($name_formatted, $s) !== false) { $word = explode($s, $name_formatted); $tmp_ary = array_map("ucfirst", array_map("strtolower", $word)); // whatever processing want here $name_formatted = implode($s, $tmp_ary); } } return $name_formatted; } echo format_name($name_unformatted); ?>
you can expand on function handle name exceptions.
Comments
Post a Comment