我有一个用PHP编写的文本字符串:
<strong> MOST </strong> of you may have a habit of wearing socks while sleeping.
<strong> Wear socks while sleeping to prevent cracking feet</strong>
<strong> Socks helps to relieve sweaty feet</strong>我们可以看到,第一个强标记是
<strong> MOST </strong>我想删除第一个强标签,并将其内部的单词转换为ucword(第一个字母大写)。结果如下所示
Most of you may have a habit of wearing socks while sleeping.
<strong> Wear socks while sleeping to prevent cracking feet</strong>
<strong> Socks helps to relieve sweaty feet</strong>我已经尝试了爆炸功能,但它似乎不是我想要的。以下是我的代码
<?php
$text = "<strong>MOST</strong> of you may have a habit of wearing socks while sleeping. <strong> Wear socks while sleeping to prevent cracking feet</strong>. <strong> Socks helps to relieve sweaty feet</strong>";
$context = explode('</strong>',$text);
$context = ucwords(str_replace('<strong>','',strtolower($context[0]))).$context[1];
echo $context;
?>我的代码只有结果
Most of you may have a habit of wearing socks while sleeping. <strong> Wear socks while sleeping to prevent cracking feet发布于 2013-02-02 12:56:28
您可以使用explode的可选限制参数来修复代码
$context = explode("</strong>",$text,2);但是,更好的方法是:
$context = preg_replace_callback("(<strong>(.*?)</strong>)",function($a) {return ucfirst($a[1]);},$text);发布于 2013-02-02 12:58:17
我知道您要求用PHP提供解决方案,但我不认为向您展示CSS解决方案会有什么坏处:
HTML
<p><strong>Most</strong> of you may have a habit of wearing socks while sleeping.</p>CSS
p strong:first-child {
font-weight: normal;
text-transform: uppercase;
}除非有特定的原因使用PHP,否则我认为它只会使本应简单的事情变得复杂。使用CSS可以减少服务器负载,并使样式保持在应有的位置。
更新: Here's a fiddle.
发布于 2013-02-02 12:57:26
这将是有意义的:
preg_replace("<strong>(.*?)</strong>", "$1", 1)https://stackoverflow.com/questions/14658732
复制相似问题