PHP - 改进文本大小写功能

时间:2015-03-31 10:35:05

标签: php regex capitalization

我有一个大写字符串的函数:

function capitalize_sentence($text)
    {
      $output =  preg_replace_callback('/([.!?])\s*(\w)/', function ($matches) {
            return strtoupper($matches[1] . ' ' . $matches[2]);
        }, ucfirst(strtolower($text)));
        return $output;
    }

当我有一个像这样的简单字符串时:

$text = 'hello. this works !';
var_dump($text);

$text = capitalize_sentence($text);
var_dump($text);die;

这很好用:

string 'hello.this works !' (length=18) 

string 'Hello. This works !' (length=19)

但是在我的代码中,有时候,字符串看起来像这样(带有一些标签):

$text = '<span>hello.</span> this <b>works</b> !';
var_dump($text);

$text = capitalize_sentence($text);
var_dump($text);die;

这给了我这个(正如你所看到的,第一个单词没有大写......):

string '<span>hello.</span> this <b>works</b> !' (length=39)

string '<span>hello.</span> this <b>works</b> !' (length=39)

如何改进我的代码?我需要逃避&#34; <tags>没有删除它们但是将第一个单词大写,如第一个示例所示....

我需要这样的输出:

string '<span>Hello.</span> This <b>works</b> !' (length=39)

谢谢!

3 个答案:

答案 0 :(得分:2)

试试这个:

function ucSentence($str) {
    $len = strlen($str);
    $flagNeedUC = TRUE; // start of sentence flag
    $flagTag = FALSE;   // inside tag flag
    $endOfSentence = array('.', '!', '?');
    for ($ix = 0; $ix < $len; $ix += 1) {
        if ($flagTag) {
            if ('>' === $str{$ix}) { // resolve end tag
                $flagTag = FALSE;
            }
        } else {
            if (in_array($str{$ix}, $endOfSentence)) { // resolve end sentence
                $flagNeedUC = TRUE;
            } elseif ('<' === $str{$ix}) { // resolve start tag
                $flagTag = TRUE;
            } elseif (ctype_alpha($str{$ix}) && $flagNeedUC) { // resolve first char after sentence end
                $flagNeedUC = FALSE;
                $str{$ix} = strtoupper($str{$ix});
            }
        }
    }
    return $str;
}
echo ucSentence('<span><b>hello. </b></span> this <b>works</b> !');

打印<span><b>Hello. </b></span> This <b>works</b>

更新 特别是@ w35l3y:)

我添加了传递属性值。它识别在野外互联网中出现的几种形式的属性值: <tag attr="value"><tag attr='value'><tag attr=value attr=value>

function ucSentence($str) {
    $len = strlen($str);
    $flagNeedUC = TRUE; // start of sentence flag
    $flagTag = FALSE;   // inside tag flag
    $stageAttr = FALSE;  // inside attribute value
    $endOfSentence = array('.', '!', '?');
    for ($ix = 0; $ix < $len; $ix += 1) {
        if ($flagTag) {
            if ($stageAttr) {
                if ('=' === $stageAttr) {
                    if ('"' === $str{$ix}) {
                        $stageAttr = '"';
                    } elseif ('\'' === $str{$ix}) {
                        $stageAttr = '\'';
                    } else {
                        $stageAttr = ' >';                        
                    }
                } elseif (strpos($stageAttr, $str{$ix}) !== FALSE) {
                    if ('>' === $str{$ix}) {
                        $flagTag = FALSE;
                    }
                    $stageAttr = FALSE;
                }
            } else {
                if ('>' === $str{$ix}) { // resolve end tag
                    $flagTag = FALSE;
                } elseif ('=' === $str{$ix}) {
                    $stageAttr = '=';
                }
            }
        } else {
            if (in_array($str{$ix}, $endOfSentence)) { // resolve end sentence
                $flagNeedUC = TRUE;
            } elseif ('<' === $str{$ix}) { // resolve start tag
                $flagTag = TRUE;
            } elseif (ctype_alpha($str{$ix}) && $flagNeedUC) { // resolve first char after sentence end
                $flagNeedUC = FALSE;
                $str{$ix} = strtoupper($str{$ix});
            }
        }
    }
    return $str;
}

$testArr = array(
    '<span><b>hello. </b></span> this <b>works</b> !',
    'test. <span title="jane <3 john"> <b>hello. </b></span> this <b>works</b> !',
    'test! <span title="hover -> here"> <b>hello. </b></span> this <b>works</b> !',
    'test <span title="jane <3 john"> <b>hello. </b></span> this <b>works</b> !',
    'test? <span title="hover -> here"> <b>hello. </b></span> this <b>works</b> !',
    'test <span title="hover -> here"> <b>hello. </b></span> this <b>works</b> !',
    'test. <span title=\'hover -> here\'> <b>hello. </b></span> this <b>works</b> !',
    'test. <span title=jane<3john data=jane> <b>hello. </b></span> this <b>works</b> !',
);
foreach ($testArr as $num => $testStr) {
    printf("[%d] %s\n", $num, ucSentence($testStr));
}

打印:

[0] <span><b>Hello. </b></span> This <b>works</b> !
[1] Test. <span title="jane <3 john"> <b>Hello. </b></span> This <b>works</b> !
[2] Test! <span title="hover -> here"> <b>Hello. </b></span> This <b>works</b> !
[3] Test <span title="jane <3 john"> <b>hello. </b></span> This <b>works</b> !
[4] Test? <span title="hover -> here"> <b>Hello. </b></span> This <b>works</b> !
[5] Test <span title="hover -> here"> <b>hello. </b></span> This <b>works</b> !
[6] Test. <span title='hover -> here'> <b>Hello. </b></span> This <b>works</b> !
[7] Test. <span title=jane<3john data=jane> <b>Hello. </b></span> This <b>works</b> !

答案 1 :(得分:1)

尝试此更新,我添加了更多条件并更改了替换:

 $output =  preg_replace_callback('/((?:^|[.!?])(?:<[^>]*?>)?)(\s*)(\w)/', function ($matches) {
        return $matches[1] . $matches[2] . strtoupper($matches[3]);
    }, ucfirst(strtolower($text)));

输出<span>Hello.</span> This <b>works</b> !

答案 2 :(得分:0)

这是@tutankhamun的经过稍微修改的版本,可防止在电子邮件地址或URL中的句点之后大写字符(或在句子字符末尾(。!?)后没有空格的其他任何时间)

function sentenceCase($str) {
    $len = strlen($str);
    $flagNeedUC = TRUE; // start of sentence flag
    $flagTag = FALSE;   // inside tag flag
    $stageAttr = FALSE;  // inside attribute value
    $lastChar = NULL;
    $endOfSentence = array('.', '!', '?');
    for ($ix = 0; $ix < $len; $ix += 1) {
        if ($flagTag) {
            if ($stageAttr) {
                if ('=' === $stageAttr) {
                    if ('"' === $str{$ix}) {
                        $stageAttr = '"';
                    } elseif ('\'' === $str{$ix}) {
                        $stageAttr = '\'';
                    } else {
                        $stageAttr = ' >';
                    }
                } elseif (strpos($stageAttr, $str{$ix}) !== FALSE) {
                    if ('>' === $str{$ix}) {
                        $flagTag = FALSE;
                    }
                    $stageAttr = FALSE;
                }
            } else {
                if ('>' === $str{$ix}) { // resolve end tag
                    $flagTag = FALSE;
                } elseif ('=' === $str{$ix}) {
                    $stageAttr = '=';
                }
            }
        } else {
            if (in_array($str{$ix}, $endOfSentence)) { // resolve end sentence
                $flagNeedUC = TRUE;
            } elseif ('<' === $str{$ix}) { // resolve start tag
                $flagTag = TRUE;
            } elseif (ctype_alpha($str{$ix}) && $flagNeedUC) { // resolve first char after sentence end
                $flagNeedUC = FALSE;
                if (!in_array($lastChar, $endOfSentence)) $str{$ix} = strtoupper($str{$ix});
            }
        }
        $lastChar = $str{$ix};
    }
    return $str;
}