?
Solved

Search and Replace Function...

Posted on 2008-10-05
7
Medium Priority
?
191 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 1400 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 600 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 111

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 1400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
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…
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

862 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