Adding span tags to post titles using regex

My basic titles are something like

Part One: Part Two

And I’m trying to end up with something like this using the colon as what I find in the regex:

<span class="one-class">Part One:</span><br><span class="two-class">Part Two</span>

This is the original in entry-header.php and I want to continue to have that html:

if ( is_singular() ) {
            the_title( '<h1 class="entry-title">', '</h1>' );
        }

The following works as long as there is a colon in the title. If there is no colon, then none of the html gets added.

if ( is_singular() ) {
                        $string = get_the_title();
            $pattern = '~(.+): (.+)~i';
            $replacement = '<h1 class="entry-title"><span class="title-cite-pali">$1:</span><br><span class="title-english">$2</span></h1>';
            echo preg_replace($pattern, $replacement, $string);
        }

But I think what I really want is the following, but it doesn’t work. The output is as if my added code is not there.

if ( is_singular() ) {
            $string = the_title( '<h1 class="entry-title test">', '</h1>' );
            $pattern = '~(.+): (.+)~i';
            $replacement = '<span class="title-cite-pali">$1:</span><br><span class="title-english">$2</span>';
            echo preg_replace($pattern, $replacement, $string);
            
        }

I think if I could get the above code working, it is preferable since if there was no colon at least the h1 tags would be added.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It’s nice to use regex, but you can also achieve the same thing here by just splitting the string on ':' with explode e.g.:

    $matches = explode(":", get_the_title()) ;

    if (count($matches) == 2) {
       $result = "foo bar $matches[0] foo bar $matches[1]";
    } else {
       // case with no : in title
       $result = "foo bar $matches[0] foo bar";
    }
    echo $result;

Method 2

You don’t need a regex.

First, grab the title as a string variable:

$title = get_the_title();

Then, find the location of the first colon:

$colon = strpos( $title, ':' );

If there is no colon, handle that:

if ( $colon === FALSE ) {
    // there was no colon, handle that!
    echo '<h2>' . esc_html( $title ) . '</h2>';
} else {
    // the rest of the code
}

What about the rest of the code that goes in the else? Well, lets split it in two:

$first_part = substr( $title, 0, $colon );
$second_part = substr( $title, $colon + 1, strlen( $title ) );

Now we can output them differently:

echo '<span>' . $first_part . '</span>';
echo '<span>' . $second_part . '</span>';


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x