Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8068535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:43:17+00:00 2026-06-05T12:43:17+00:00

Problem: I am stuck in figuring out how to add a string in a

  • 0

Problem:

I am stuck in figuring out how to add a string in a variable to a number of different strings using PHP.

Variable:

$insert = 'icon-white';

Strings are in a variable called $hyperlink:

$hyperlink = '<i class="icon-home"></i>';

Desired output:

<i class="icon-home icon-white"></i>

Any recommendations are welcome, and thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:43:20+00:00Added an answer on June 5, 2026 at 12:43 pm

    I honestly don’t see the benefits of regex in this particular question, so I’ve chosen to disregard that aspect; the principal concern seems to be to insert the new string before the last " character, which can be achieved with the following:

    $hyperlink = '<i class="icon-home"></i>';
    $insert = ' icon-white'; // I've explicitly prefixed the new string with a space
    $pos = strripos($hyperlink,'"',0);
    echo substr_replace($hyperlink,$insert,$pos,0)
    

    If you’d rather, then, for future use, here’s a function that will insert a given string ($new) into another string ($haystack) before the last occurrence of a given character ($needle):

    function insertBeforeLast($haystack,$needle,$new){
        if (!$haystack || !$needle || !$new){
            return false;
        }
        else {
            return substr_replace($haystack,$new,strripos($haystack,$needle),0);
        }
    }
    
        echo insertBeforeLast('abcdefg','e','12',' ');
    

    The 0 before the closing parenthesis of substr_replace() in the function denotes the number of characters that the newly-inserted string will overwrite in the original string.


    Edited to amend the above function to explicitly offer the over-writing as an option:

    function insertBeforeLast($haystack,$needle,$new, $over){
        if (!$haystack || !$needle || !$new){
            return false;
        }
        else {
            $over = $over || 0;
            return substr_replace($haystack,$new,strripos($haystack,$needle),$over);
        }
    }
    
        echo insertBeforeLast('abcdefg','e','12',0);
    

    References:

    • strripos().
    • substr_replace().
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently somewhat stuck figuring out an elegant solution to my following problem:
I have been stuck on this problem for quite awhile... I hope someone out
Problem I'm currently stuck trying to figure out what iPhone OS Deployment Target setting
I'm stuck figuring out the best practice... I want to create a following system
I'm stuck at yet another C problem. How can I concatenate two strings with
After switching to AS3, I've been having a hell of a time figuring out
I'm having trouble figuring out how best to model my data. I have the
I'm currently working on my own PHP Framework, and I need some help figuring
First time poster, long time reader. I've been having a problem with figuring this
I got a problem stuck me few days, tried several ways still can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.