Solved

PHP - Preg_replace() make replacement with array value

Posted on 2011-02-17
4
497 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 250 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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

706 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now