2

我是字符串操作的新手,只是想替换列表中的值。

我要修复的两个输入是MCAFEEPORT O'BRIAN

所以我跑ucwords(strtolower($rawTitle))。但是现在我有Mcafee并且Port O'brian当我需要显示McAfeeand时Port O'Brian

首先关注Port O'brian,这是我的尝试,但显然它不起作用,因为它大写字符串的第三个字母而不是第三个匹配的字符,就像我想要的那样:(

$oPattern = "/O\'[a-z]/"; //Pattern to match
$doesMatch = preg_match($oPattern, $output, $matches); //Do I need to the perform operation?
if ($doesMatch == 1) {
    $letters = str_split($output);//break it out into an array
    $cappedLetter = strtoupper($letters[2]);//capitalize the 3rd letter
    $output = preg_replace("/O'$letters[2]/", $output, "O'$cappedLetter");//replace the O'x with O'X
    return $output;
}

我应该在这里使用不同的功能吗?

4

3 回答 3

2

不,如果字符串中没有大小写信息,则没有通用的方法可以做到这一点。(例如,考虑姓氏 O'Hara/O'hara 的两种可能的大小写。)您可以尽可能地尝试:

$title = ucwords(strtolower($rawTitle));
$title = preg_replace_callback('/(?<=O\'|Mc)./', function($match) {
    return strtoupper($match[0]);
}, $title);

但请确保之后手动检查它们。

于 2012-12-26T19:52:50.630 回答
1

名称大写的规则可能相当复杂。这是我如何处理这些问题的教学示例。请参阅http://www.laprbass.com/RAY_capitalize_names.php

<?php // RAY_capitalize_names.php
error_reporting(E_ALL);

// SOME TEST NAMES
$names = array
( "o'brien"
, 'MCAFEE'
, "barrett-o'reilly"
, "smith jones"
, "burns"
, "CROWTHER"
, "George w. bush, iiI"
, "RONALD    MCDONALD"
, "RONALD    MCDONALD-o'brien"
, "van De Graaff GeneratoR"
)
;

// TEST EACH CASE
foreach ($names as $name)
{
    echo "<br/>$name ";
    echo fixname($name);
}


// FUNCTION TO HANDLE NAMES
function fixname($name)
{
    // SPECIAL CASES FOR UPPER OR LOWER CASE DISPOSITION
    $uc = array  // UPPERCASE AFTER ANY OF THESE
    ( 'Mc'
    , "'"
    , '-'
    )
    ;

    $lc = array  // ALWAYS LOWER CASE
    ( 'Van De '
    )
    ;

    $mc = array  // ALWAYS UPPER CASE
    ( 'Iii'
    )
    ;

    // REMOVE UNNECESSARY BLANKS
    $name = preg_replace('/\s\s+/', ' ', $name);

    // START WITH LOWER CASE AND UPPER FIRST
    $name = strtolower($name);
    $name = ucwords($name);

    // CHECK FOR KNOWN SPECIAL UPPER-CASES
    foreach ($uc as $dlm)
    {
        // FIX THE Mcdonald EXAMPLE, ETC
        $namex = explode($dlm, $name);
        foreach ($namex as $k => $v)
        {
            $namex[$k] = ucwords($v);
        }
        $name = implode($dlm, $namex);
    }

    // CHECK FOR KNOWN CONSTANT LOWER-CASES
    foreach ($lc as $dlm)
    {
        // FIX THE van de Graaff EXAMPLE
        $name = str_replace($dlm, strtolower($dlm), $name);
    }

    // CHECK FOR KNOW CONSTANT UPPERCASE
    foreach ($mc as $dlm)
    {
        // FIX THE Bush, III EXAMPLE
        $name = str_replace($dlm, strtoupper($dlm), $name);
    }

    // RETURN THE REPAIRED STRING
    return $name;
}
于 2012-12-26T20:24:24.947 回答
0

把这个解决方案扔在那里..利用数组函数在你正在寻找的外壳中制作你的名字。它很脏,但快速有效。

1) 按'分割

2) 大写项目array_walk()

3)内爆。

4)对空格做同样的事情

function nameFix(&$name, $key)
{
    $name = ucfirst($name);
}

function nameFixer($name)
{
    $array = explode(" ", $name);
    array_walk(
            $array, 
            function (&$name, $key) { 
                $name = explode("'", $name); 
                array_walk($name, 'nameFix'); 
                $name = implode("'", $name); 
            }
    );
    array_walk($array, nameFix);
    return implode(" ", $array);
}

echo nameFixer("mister port o'hare o'brian fredrickson"); 
// Returns: Mister Port O'Hare O'Brian Fredrickson
于 2012-12-26T20:08:09.880 回答