Solved

Search and Replace Function...

Posted on 2008-10-05
7
185 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL Sub-Select Query Returning Duplicate Result 7 45
How to fix Datetime in MySQL? 4 48
Need to remove %20 from url in get 17 36
Test if unique ID is in log file 5 21
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 …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

860 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