0

我目前正试图让任何/所有我的 Joomla 文章/类别/博客标题的第一个单词都有一种颜色,然后句子的其余部分是网站默认值。我发现下面的代码确实改变了颜色,但它仅在标题包含 2 个单词并且如果有更多单词时才有效,则它会删除所有格式。

<?php if ($this->params->get('show_page_heading')) : ?>
    <?php 
    $title = $this->escape($this->params->get('page_heading'));
            $title = str_replace(' ', '<span>', $title);
            echo "<h1>" . $title . "</h1>";
    ?>
<?php endif; ?>

谢谢!

4

1 回答 1

0

在这种情况下,您必须考虑标题是 1 个单词或多个单词。

尝试这个..

// check to see if there are multiple words by the count of the space character 
if(substr_count($title,' ') > 0) {
    // multiple words
    // replace the FIRST space with closing span tag
    $title = '<span>'.preg_replace('/\ /', '</span> ', $title, 1);
}
else {
    // one word, just close the span
    $title = '<span>'.$title.'</span>';
}
echo "<h1>" . $title . "</h1>";

请注意,如果标题中的第一个字符是空格,那么您将得到一个空跨度而不是所需的效果。

于 2013-06-26T13:49:50.803 回答