簡體   English   中英

首字母大寫

[英]Capitalize first letter in <em>

$item = 'some <html> goes <div class="here"> and </div> can be placed <em>     some words</em>, and then more </html> can exist';

如何僅將<em>...</em>第一個單詞的首字母大寫?

您可以使用正則表達式 ,如下所示:

function replaceStuff($matches){
        return $matches[1].strtoupper($matches[2]);
}

$item=preg_replace_callback("/(<em[^>]*>[^\\w]*)(\\w)/i","replaceStuff",$item);

閱讀有關使用preg_replace_callback的更多信息

注意:您也可以使用CSS進行此操作; 為此,您可以使用以下代碼:

em:first-letter {
 text-transform:capitalize;
}

了解有關text-transform屬性的更多信息

這個?

function ucfirstword($str) {
   $estr = explode(" ",$str);
   $estr[0] = ucfirst($estr[0]);
   return implode(" ",$estr);
}

...
echo "<em>     ".ucfirstword("some words")."</em>";

如果只需要這個字符串,可以這樣

 $item = 'some <html> goes <div class="here"> and </div> can be placed <em>     ';
 $item .= ucfirst("some words");
 $item .= '</em>, and then more </html> can exist';
echo $item;

嘗試這個:

$item = 'some <html> goes <div class="here"> and </div> can be placed <em style="text-transform: uppercase">some</em>, and then more </html> can exist';

也許這對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM