1

Rookie needing help!

    $some_text = "This red bag will contain green apples.";
    $extracted_array = array("red","bag","green","apples","what","why"); //obtained from database

I would like to replace each occurrence of the $extracted_array values in $some_text with a hyperlinked version of itself.

So "This red bag..." becomes

    This <a href="#">red</a> bag...

This is what I wrote:

    foreach($extracted_array AS $value) {
    $new_value = '<a href="#">'.$value.'</a>';
    $new_text = str_ireplace($value, $new_value, $some_text);
            }
    print_r($linked_content);

But this simply replaces each occurrence with the last value of the loop of $extracted_array (i.e. "why")

Please show me how I can do this right.

1
  • As of PHP 5.3.0, @Flosculus answer is the best one! Commented Jan 20, 2014 at 15:47

3 Answers 3

1
$some_text = "This red bag will contain green apples.";
$extracted_array = array("red","bag","green","apples","what","why");

$expr = sprintf('/%s/i', implode('|', $extracted_array));

echo preg_replace_callback($expr, function($match){
    return sprintf('<a href="#">%s</a>', $match[0]);
}, $some_text);

Output:

This <a href="#">red</a> <a href="#">bag</a> will contain <a href="#">green</a> <a href="#">apples</a>.
Sign up to request clarification or add additional context in comments.

4 Comments

This is definetely the best & proper way to do it!
This is a very good way to do it. It answers my question, but a little problem occurs when I insert the actual hyperlinks. "This red..." should actually become:"This <a href="/something/red/something">red</a>..."
$match[0] is the match of item in $extracted_array, the return value of the callback is what that pattern will be replaced with. Im' sure you manipulate the return value further. I'm just a sprintf freak, you can use return '<a href="something/'.$match[0].'/something">'.$match[0].'</a>'
Actually, after going further with what I need to do, your way works best. I guess I need to learn working with regex more
0

I think there are better functions in PHP but I'll show you how to do it with your code:

$some_text = "This red bag will contain green apples.";
$extracted_array = array("red","bag","green","apples","what","why"); //obtained from database

foreach($extracted_array AS $value) {
    $new_value = '<a href="#">'.$value.'</a>';
    $some_text = str_ireplace($value, $new_value, $some_text);
}

print_r($some_text);

Your code takes everytime $some_text and saves it into $new_text but never changes $some_text therefor $new_text is the original of $some_text with changed last item of array.

Comments

0

You could make two arrays, and send them in to str_ireplace. However, this might get cumbersome if the total number of replacements you are doing is quite large. The function will replace the string from $aFind with the item (with the same index) from $aReplace.

$aFind = array("red", "green");
$aReplace = array("<a href='/red.php'>red</a>", "<a href='/green.php'>green</a>");
$newString = str_ireplace($aFind, $aReplace, $some_text);
print($newString);

1 Comment

Thanks, this idea worked for me, but I still used str_replace. I'll post my answer above.

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.