WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Conditionally replace text within the_title for posts (2 posts)

  1. Andrew Rowland
    Member
    Posted 1 year ago #

    I am designing a website for iPhone repair services and I want to use the CSS text-transform:uppercase for my post titles, however, I need to also maintain the lowercase"i" in iPhones. So, I thought I would be able to create a function to do this using the 'strpos' and 'preg_replace' but my code is not working and I can't figure out what is wrong with it!

    I know the expression is evaluating because I can affect the output of the 'else' - it just never sees "iphone" so it always goes to 'else' and I am not sure why.

    <h1 class="first"><?php if (strpos("the_title()",'iPhone') !== false) {
    $string = get_the_title();
    $pattern = '/iPhone/';
    $replacement = '<span id="lil-i">i</span>Phone';
    echo preg_replace($pattern, $replacement, $string);}
    else
    echo the_title();
    ?></h1>
  2. Andrew Rowland
    Member
    Posted 1 year ago #

    I figured it out!

    I had to pass the_title to a variable and then use the new variable in the strpos function.

    <?php $title= get_the_title();?>
    <h1 class="first"><?php if (strpos("$title",'iPhone') !== false) {
    $string = get_the_title();
    $pattern = '/iPhone/';
    $replacement = '<span id="lil-i">i</span>Phone';
    echo preg_replace($pattern, $replacement, $string);}
    else
    echo the_title();
    ?></h1>

Topic Closed

This topic has been closed to new replies.

About this Topic