实施例
<p>Lorem ipsum dolor sit amet</p>
<p>Morbi elementum odio vel tortor adipiscing vel tempor risus ullamcorper.</p>
我想为第一段添加类,并将span类添加到第一个字母
<p class="first-paragraph"><span class="first-letter">L</span>orem ipsum dolor sit amet</p>
让我知道如何使用PHP
答案 0 :(得分:3)
$text = '<p>Lorem ipsum dolor sit amet</p>
<p>Morbi elementum odio vel tortor adipiscing vel tempor risus ullamcorper.</p>';
$text = substr_replace($text, '</span>', 4, 0);
$text = substr_replace($text, '<span class="first-letter">', 3, 0);
$text = substr_replace($text, ' class="first-paragraph"', 2, 0);
echo htmlspecialchars($text);
: - )
答案 1 :(得分:1)
您可以使用纯CSS3:
p:first-of-type {
color: red;
}
p:first-of-type::first-letter {
color: blue;
font-size: x-large;
}