Solved

Search and Replace Function...

Posted on 2008-10-05
7
182 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 108

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

 

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 108

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

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

867 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

21 Experts available now in Live!

Get 1:1 Help Now