2013-06-27 51 views
0

当我尝试大写符号或字符后的第一个字母并且实际上效果很好但是如果我的字符串在单词之间有空格时代码忽略大写并且打印为例如:PHP符号或字符后大写

$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

怎么样一个简单的解决方案:

$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 
} 
+0

伟大的作品,但是当我试图用我的脚本中使用它,它停止工作,我知道我做错了,但我不知道问题出在哪里。 我从XML提要中获取文本@ Twisted1919 '$ str = eregi_replace(“。* ”,“”,$ part [$ i]); if(preg_match('/(^ &[^;] +;)(。*)/',$ str,$ matches)){{$ {discard,$ entity,$ text} = $ matches; // list(,$ entity,$ text)= $ matches; //这个也可以工作 $ string = $ entity。修剪(ucfirst(用strtolower($文本))); echo $ string; } else { #匹配尝试失败 }' –

+0

'eregi_replace()'后,你的'$ str'仍然包含了什么?你应该使用'preg_ *'而不是'ereg_ *'。另外,你应该在一个字符串中为你指定'$ str':'$ str =(string)preg_replace('/ [^ <] * /ix','',$ part [$ i]);'(正则表达式未经测试) – Twisted1919

相关问题