Solved

Search and Replace Function...

Posted on 2008-10-05
7
184 Views
Last Modified: 2008-10-06
I have an array like such:
$ReplaceTerms = array(
                  'dog' => 'cat',
                  'fire' => 'water'
            );

and I am looking to put together a function that I can pass the array and content to like such:

TransformInput($ReplaceTerms, $content)

The Function would then do a case insensitive search and replace on $content swapping out the text in the array. So 'dog' would become 'cat' and 'fire' would become 'water'

Make any sense?
0
Comment
Question by:interclubs
  • 4
  • 2
7 Comments
 
LVL 27

Assisted Solution

by:ddrudik
ddrudik earned 350 total points
ID: 22643857

<?php
$ReplaceTerms = array(
                  'dog' => 'cat',
                  'fire' => 'water'
            );
$testarr=Array('foo','dog','fire','foo','dog');
function TransformInput($termsarray,$subjectarray){
  foreach($termsarray as $term=>$repl){
    foreach($subjectarray as $key=>$val){
	  if($val==$term){
	    $subjectarray[$key]=$repl;
	  }
	}
  }
  return $subjectarray;
}
$testarr=TransformInput($ReplaceTerms,$testarr);
echo '<pre>'.print_r($testarr,true);
?>

Open in new window

0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22643866
You might want to compare type as well, if so change:
        if($val==$term){
to:
        if($val===$term){
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 22645146
If you are using a string for the "$content" variable, this might be what you want...

Best regards, ~Ray
function transform_input($replace_terms, $content) {
	foreach ($replace_terms as $original => $replacement) {
		$content = eregi_replace($original, $replacement, $content);
	}
return $content;
}
 
 
 
$content = "The quick brown fox jumped over the lazy dog.";
 
$replace_terms = array(
                  'dog' => 'cat',
                  'fire' => 'water'
            );
 
 
echo transform_input($replace_terms, $content);
 
?>

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:interclubs
ID: 22645162
I'm sorry, my bad, the 2nd piece pass to it ($subjectarray) won't be an array, it will be just straight text...
as in : $testarr='foo dog fire foo dog';
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 22645208
Then you should be OK with the function I posted above.  It tested out OK.  Best, ~Ray
0
 
LVL 27

Accepted Solution

by:
ddrudik earned 350 total points
ID: 22646428
str_replace has this functionality now that you might consider:
http://us3.php.net/manual/en/function.str-replace.php
From example 1:
// Provides: You should eat pizza, beer, and ice cream every day
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);

Open in new window

0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22654961
Thanks for the question and the points.
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP and google maps 13 44
javascript: add id amounts 5 47
ebay devID, appID, certID, userToken 2 27
Add email address from a web page to outlook 2010 contacts ? 8 26
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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.

770 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