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
  • It's actually an array but I use $hyperlink[0] to access the string, but for the sake of simplicity just go with that it is a string. Commented Jun 9, 2012 at 19:07

3 Answers 3

2

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:

Sign up to request clarification or add additional context in comments.

2 Comments

I HIGHLY appreciate this, it made more sense to me than the regexp, although that version works I understand what's going on in this solution. However, this is a great moment to start diving into the world of regexp.
You're very welcome; obviously I'd encourage you to consider having this as your 'accepted' answer, but I am, of course, terribly biased! Just a suggestion, though, but if you leave a comment on sputnick's answer asking for an explanation, he'll probably provide one happily enough. Regex is, after all, not easy to learn.
1

This is how to use preg_replace() php function to fit your needs :

$ php -a
Interactive shell

php > $oldvar = '<i class="icon-home"></i>';
php > $newvar = preg_replace('/(.*?".*?)"(.*)/', '\1 icon-white"\2 ', $oldvar);
php > echo $newvar;
<i class="icon-home icon-white"></i> 

2 Comments

Shouldn't the output be: <i class="icon-home icon-white"></i> ?
Your output is: <i class="icon-home" icon-white"></i> - there's an extra quote after the home word.
0

When rendering the output, you could do;

<i class="icon-home <?= $insert ?>"></i>

if you don't want it to be conditional.

If you have a variable;

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

you can do;

$i = '<i class="icon-home ${insert}"></i>';

4 Comments

You need the $ prefix for the variable (this is PHP after all), and I'd really suggest not using short-tags.
I have updated the question. The strings are in a variable and I'm looking for a preg solution where it looks for the last quote and insert " icon-white" before it.
Thanks for that catch David Thomas
David Thomas, intrigued as to why you wouldn't use short-tags, I did some searching around.I assume you recommended it because it's not always turned on.Doing a little research,I found that they're not part of short-tags anymore.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.