0

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.

3
  • 1
    If you want the_title() function to return its result instead of echoing it, you should use $string = the_title( '<h1 class="entry-title test">', '</h1>', false ); Commented Jul 7, 2020 at 15:04
  • 1
    so do you need to allow for the case with no colon? what should happen then? why not just add that case to your first piece of code if it's working ;-)
    – mozboz
    Commented Jul 7, 2020 at 15:11
  • 1
    You don't need regex, just split the string by the : character and wrap the array items in span tags. A regular expression is just overcomplicating it ( and you shouldn't parse HTML with regex anyway )
    – Tom J Nowell
    Commented Jul 7, 2020 at 15:30

2 Answers 2

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;
4
  • Thanks! Any time regex can be avoided I guess it should. I've tried to implement your solution but WP is kicking out the following error: Parse error: syntax error, unexpected '==' (T_IS_EQUAL) in ... line 48. That line is if (count($matches)) == 2) Commented Jul 7, 2020 at 15:39
  • 1
    oops, yep one too many ) .. updated the answer, try again ;-)
    – mozboz
    Commented Jul 7, 2020 at 15:43
  • That did it! Thanks! I think I will go with your solution as it seems to be the most straightforward to understand. Commented Jul 7, 2020 at 15:52
  • @HopefullCoder cool, glad it worked. Upvotes and accepted answers appreciated
    – mozboz
    Commented Jul 7, 2020 at 16:07
1

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>';
1
  • Thank you! I think I will go with the explode answer since it is simpler, but I may need to return to your answer if my requirements get more complicated. Commented Jul 8, 2020 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.