Solved

PHP - Preg_replace() make replacement with array value

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.
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.

730 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