Needle in a haystack
Moderator: General Moderators
-
spacebiscuit
- Forum Contributor
- Posts: 390
- Joined: Mon Mar 07, 2005 3:20 pm
Needle in a haystack
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.
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.
Re: Needle in a haystack
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
So the replaced text is contained in the $count variable?
Am I reading the code correctly?
Thanks,
Rob.
Am I reading the code correctly?
Thanks,
Rob.
Re: Needle in a haystack
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).robburne wrote:So the replaced text is contained in the $count variable?
-
spacebiscuit
- Forum Contributor
- Posts: 390
- Joined: Mon Mar 07, 2005 3:20 pm
Re: Needle in a haystack
I've tried fitting your suggestion into my code but I can't see how it will work:
Any ideas?
Thanks,
Rob.
Code: Select all
$words=getwords();
$text=str_replace($words,"****",$text,$count);Thanks,
Rob.
Re: Needle in a haystack
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
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.
The stack is a paragraph of text, the needle is an array as you have seen.
Rob.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Needle in a haystack
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
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.
There must be an easier way of doing this using a function - back to the drawing board.
Thanks for your help.
Rob.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Needle in a haystack
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);Re: Needle in a haystack
$replace[$i] wouldn't make sense in your code since $replace is a string.Jonah Bron wrote:You mean you tried this?
-
spacebiscuit
- Forum Contributor
- Posts: 390
- Joined: Mon Mar 07, 2005 3:20 pm
Re: Needle in a haystack
Yes that is correct - I am only replacing the needle with a string of '****'.
Here is my exact code:
It gives some odd output:

It really does not work.
Rob.
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 really does not work.
Rob.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Needle in a haystack
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
So why when I output the replaced array variable are three text boxes being drawn?
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Needle in a haystack
Those are not caused by the code here. They're coming from somewhere else. Maybe you could post the rest of your code?