Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

str_replace aray

Posted on 2008-10-13
4
Medium Priority
?
236 Views
Last Modified: 2009-11-01
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.
0
Comment
Question by:deliriumxx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
hampus_b earned 252 total points
ID: 22709232
Replace in two steps:
<?
 
$seed = "seed and here is base and here is original";
$search = array("seed","base","original");
$replacetemp = "{TEMP}";
$replace = array("seed","base","original");
$params = explode('|', $perm);
 
 
$variant = str_replace($search, $replacetemp, $seed);
$variant = str_replace($replacetemp, "<b>{".join("|",$replace)."}</b>", $variant);
echo "<br>";
echo $variant;
?>

Open in new window

0
 
LVL 12

Expert Comment

by:jet-black
ID: 25315985
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

0
 
LVL 12

Assisted Solution

by:jet-black
jet-black earned 248 total points
ID: 25315994

<?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

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question