2013-06-27 49 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

相關問題