我正在尝试在<strong>
中找到句子中的所有字符串。没有任何运气试了这个:
function getTextBetweenTags($string, $tagname)
{
$pattern = "/<$tagname>(.*?)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return ucfirst($matches[1]);
}
$sentence = "Yellow pitty lies <strong>about</strong> the life.";
$finalsentence = getTextBetweenTags($sentence,"strong");
这样做的正确方法是什么?
答案 0 :(得分:3)
有一种更简单的方法。而不是使用PHP,你只能使用css,例如:
strong:first-letter{
text-transform: capitalize
}
答案 1 :(得分:1)
您需要在标记之前和之后包含匹配文字。
function getTextBetweenTags($string, $tagname)
{
$pattern = "/(.*<$tagname>)(.*?)(<\/$tagname>.*)/";
preg_match($pattern, $string, $matches);
return $matches[1] . ucfirst($matches[2]) . $matches[3];
}
$sentence = "Yellow pitty lies <strong>about</strong> the life.";
$finalsentence = getTextBetweenTags($sentence,"strong");