Link to home
Start Free TrialLog in
Avatar of deliriumxx
deliriumxx

asked on

str_replace aray

Hi Experts,

I will explain the code I am working on,,, and all help is much appreciated.

Here is an oversimplified version of the code I am working with:

$seed = "seed and here is base and here is original";
$perm = "seed|base|original"; //This is the replacement string
$params = explode('|', $perm);
//This is the output
$variant = str_replace($params, "<b>{".$perm."}</b>", $seed);
echo $variant;


If you run the code I supplied,,,, You will notice that I am using $seed as a string that I am searching through in order to find any variation of the matching occurrences from $perm.

In essence I have to split $perm which I do in the$ params,, then find a match to any of the values in the $params array and replace those values with the $perm again.

In the way I am doing it now,,, the result turns out like this:
{seed|{seed|base|{seed|base|original}}|{seed|base|original}} and here is {seed|base|{seed|base|original}} and here is {seed|base|original}

What I need for the result is this:
{seed|base|original} and here is {seed|base|original} and here is {seed|base|original}

I do not have the option to manipulate the order in which various strings appear in the $seed string nor in $perm.

All help with this is greatly appreciated :)

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of hampus_b
hampus_b
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ovunc Tukenmez
You can try with preg_replace:

<?php
$seed = "seed and here is base and here is original";
$perm = "seed|base|original"; //This is the replacement string
 
//This is the output
$variant = preg_replace("/$perm/", "<b>{$perm}</b>", $seed);
echo $variant;
?>

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial