0

Im currently trying to make the first word of any/all my Joomla Articles/Categories/Blog headings have one color and then the rest of the sentence be the site default. I have found the code below that does change the color but it only works if the heading contains 2 words and if there are more then it removes all formatting.

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

Thanks!

1 Answers1

0

In this scenario, you must account for the heading being either 1 word or multiple words.

Try this..

// 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>";

Be mindful that if the first character in the heading is a space, then you will get an empty span and not the desired effect.