Needle in a haystack

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Needle in a haystack

Post by spacebiscuit »

Hi,

I'm using str_replace to search for an occurence of a string in a body of text. Does anyone know if it is possible to capture the text match is found. str_replace only seems to replace what has been found - I want to record and act upon which needle is found in the haystack.

Thanks in advabnce,

Rob.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Needle in a haystack

Post by Weirdan »

like this:

Code: Select all

$search = array('this', 'something');
$replace = array('that', 'nothing');
$replaced = array();
$count = 0;
$str = "something goes somewhere";
foreach ($search as $i => $needle) {
  $str = str_replace($needle, $replace[$i], $str, $count);
  $replaced[$needle] = $count;
}
var_dump($replaced);
?
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Needle in a haystack

Post by spacebiscuit »

So the replaced text is contained in the $count variable?

Am I reading the code correctly?

Thanks,

Rob.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Needle in a haystack

Post by Weirdan »

robburne wrote:So the replaced text is contained in the $count variable?
Nope, count contains the number of occurrences that were replaced. Replaced text is known because that's what was searched for ($needle variable in the code snippet above).
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Needle in a haystack

Post by spacebiscuit »

I've tried fitting your suggestion into my code but I can't see how it will work:

Code: Select all

$words=getwords();
$text=str_replace($words,"****",$text,$count);
Any ideas?

Thanks,

Rob.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Needle in a haystack

Post by Weirdan »

Assuming $words is an array you would need to iterate over it and replace every word one by one, tracking the number of each word occurrences (hint: foreach in my snippet was there for a reason).
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Needle in a haystack

Post by spacebiscuit »

i am not sure how I can use for each in my case.

The stack is a paragraph of text, the needle is an array as you have seen.

Rob.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Needle in a haystack

Post by Jonah Bron »

Use all the code Weirdan provided, but replace the value of $search with getwords(), and replace the value of $replace with "****", or whatever.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Needle in a haystack

Post by spacebiscuit »

Sorry that doesn't work - I am getting some strange behaviour.

There must be an easier way of doing this using a function - back to the drawing board.

Thanks for your help.

Rob.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Needle in a haystack

Post by Jonah Bron »

You mean you tried this?

Code: Select all

$search = getwords();
$replace = '****';
$replaced = array();
$count = 0;
$str = "something goes somewhere";
foreach ($search as $i => $needle) {
  $str = str_replace($needle, $replace[$i], $str, $count);
  $replaced[$needle] = $count;
}
var_dump($replaced);
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Needle in a haystack

Post by Weirdan »

Jonah Bron wrote:You mean you tried this?
$replace[$i] wouldn't make sense in your code since $replace is a string.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Needle in a haystack

Post by spacebiscuit »

Yes that is correct - I am only replacing the needle with a string of '****'.

Here is my exact code:

Code: Select all

$search=getwords();
$replace='****';
$replaced = array();
 $count = 0;

foreach ($search as $i => $needle){
     $text=str_replace($needle,$replace,$text,$count);
     $replaced[$needle] = $count;
                                                  }

var_dump($replaced);
It gives some odd output:

Image

It really does not work.

Rob.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Needle in a haystack

Post by Jonah Bron »

It _does_ work. Everything you want is in those variables. Play around with it. To remove that output, just remove the var_dump() line. That is a debugging function that outputs the value of a variable.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Needle in a haystack

Post by spacebiscuit »

So why when I output the replaced array variable are three text boxes being drawn?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Needle in a haystack

Post by Jonah Bron »

Those are not caused by the code here. They're coming from somewhere else. Maybe you could post the rest of your code?
Post Reply