Solved

PHP - Preg_replace() make replacement with array value

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery detect if it is a mobile device 3 90
How can I run the php script or the page without visiting the page? 6 50
Update from TABLE-A to TABLE-B 5 34
html input type 3 21
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 discusses how to create an extensible mechanism for linked drop downs.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

930 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

10 Experts available now in Live!

Get 1:1 Help Now