Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP - Preg_replace() make replacement with array value

Posted on 2011-02-17
4
Medium Priority
?
516 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
[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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

610 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