简体   繁体   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>第一个单词的首字母大写?

You can use a regular expression , like this: 您可以使用正则表达式 ,如下所示:

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

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

Read more about using preg_replace_callback . 阅读有关使用preg_replace_callback的更多信息

Note: you can also do this using CSS; 注意:您也可以使用CSS进行此操作; to do this, you can use this code: 为此,您可以使用以下代码:

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

Read more about the text-transform property . 了解有关text-transform属性的更多信息

this? 这个?

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

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

if you need only this string, you can do like this 如果只需要这个字符串,可以这样

 $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;

Try this: 尝试这个:

$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';

Maybe this helps you. 也许这对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM