?
Solved

PHP - Preg_replace() make replacement with array value

Posted on 2011-02-17
4
Medium Priority
?
519 Views
Last Modified: 2012-05-11
Hello Experts

I am trying to do another reg_exp thing

I have an array....

  array['red food']=>"apples";
  array['yellow food']=>"bananas";  
  array['green food']=>"peas";
 
and an input string....

  $string = "i like <like=\"yellow food\"> and i like <like=\"green food\">";
 
  The replacement is done so that the output reads...
 
  i like bananas amd i like peas

I have been trying to do this in one line in the preg_replace() but i dont seem to be able to do it correctly.
Does anyone have ideas or will i have to do it in two stages...


thanks
0
Comment
Question by:handypam
  • 2
4 Comments
 
LVL 9

Expert Comment

by:AriMc
ID: 34922308
You didn't provide any code as an example of how you are trying to do it. That would really help.

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34922377
$my_array['red food']="apples";
$my_array['yellow food']="bananas";
$my_array['green food']="peas";

$string = "i like <like=\"yellow food\"> and i like <like=\"green food\">";

print preg_replace('/<like="([^"]*)">/e', "\$my_array['$1']", $string);

Result:
i like bananas and i like peas
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 1000 total points
ID: 34922403
Note that to prevent values like "purple food" being swapped with a blank, you might need to tighten the pattern up like this:

$my_array['red food']="apples";
$my_array['yellow food']="bananas";
$my_array['green food']="peas";

$string = "i like <like=\"yellow food\"> and i like <like=\"green food\">, but not so much <lik
e=\"purple food\">\n";

$pattern = '/<like="('.implode("|",array_keys($my_array)).')">/e';
print preg_replace($pattern, "\$my_array['$1']", $string);

Result:
i like bananas and i like peas, but not so much <like="purple food">
0
 

Author Closing Comment

by:handypam
ID: 34922485
thank you so much
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month16 days, 20 hours left to enroll

864 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