当我尝试将符号或字符后面的第一个字母大写并且实际效果很好但是如果我的字符串在单词之间有空格而代码忽略de uppercase并按原样打印时,我遇到了问题,例如:
$string = "•TEXT";
$string = ucfirst(strtolower($string));
$string = preg_replace_callback('/[.!?].*?\w/', create_function('$matches', 'return strtoupper($matches[0]);'),$string);
$string = preg_replace('/^•([a-z])([a-z]+)$/e', '"•" . strtoupper("\\1") . "\2"', $string);
echo $string; //This print •Text
上面的示例正确显示了文本,但没有空格。如果像下一个代码那样有空格,则以小写形式显示所有文本,例如:
$string = "•TEXT EXAMPLE";
$string = ucfirst(strtolower($string));
$string = preg_replace_callback('/[.!?].*?\w/', create_function('$matches', 'return strtoupper($matches[0]);'),$string);
$string = preg_replace('/^•([a-z])([a-z]+)$/e', '"•" . strtoupper("\\1") . "\2"', $string);
echo $string; //This print •text example (This is my problem, all is lowercased)
你能帮我解决这个问题吗?
答案 0 :(得分:0)
更简单的解决方案:
$str = '•TEXT EXAMPLE';
if (preg_match('/(^&[^;]+;)(.*)/', $str, $matches)) {
list($discard, $entity, $text) = $matches;
// list(, $entity, $text) = $matches; // this works too
$string = $entity . trim(ucfirst(strtolower($text)));
echo $string;
} else {
# Match attempt failed
}