Solved

Search and Replace Function...

Posted on 2008-10-05
7
181 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 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

15 Experts available now in Live!

Get 1:1 Help Now