开发者

Can php filter words in a string while converting the remainder of the string

I am using this function in my form to handle text conversion. This is an internal (workplace) web form. It converts

main st./grand ave. -> Main St./Grand Ave.

apt. #a1 -> Apt. #A1

1234 w. main st (childrens park) -> 1234 W. Main St (Childrens Park)

smith-o'rourke -> Smith-O'Rourke

mcbride-macgregor -> McBride-MacGregor

macias-machado -> MacIas-MacHado.

Hence, my problem!!

How do I include a filter list of names or words that should not be converted, such as Macias, Macayo, Machado, Mack, machine, etc.?

function uc_names($name_text) {
    $name_text = strtolower($name_text);
    $name_text = join("#", array_map('ucwords', explode("#", $name_text)));
    $name_text = join("/", array_map('ucwords', explode("/", $name_text)));
    $name_text = join("(", array_map('ucwords', explode("(", $name_text)));
    $name_text = join("'", array_map('ucwords', explode("'", $name_text)));
    $name_text = join("-", array_map('ucwords', explode("-", $name_text)));
    $name_text = join("Mc", array_m开发者_运维技巧ap('ucwords', explode("Mc", $name_text)));
    $name_text = join("Mac", array_map('ucwords', explode("Mac", $name_text)));
    return trim($name_text);
    }


How about something like this?

$bar = 'HELLO WORLD!';
$bar = ucwords($bar);             // HELLO WORLD!
$bar = ucwords(strtolower($bar)); // Hello World!

Or something in this direction?

function ucsmart($text)
{
   return preg_replace('/([^a-z]|^)([a-z])/e', '"$1".strtoupper("$2")', strtolower($text));
}

I haven't test the second one

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜