Page 1 of 2
Needle in a haystack
Posted: Thu Mar 17, 2011 3:36 pm
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.
Re: Needle in a haystack
Posted: Thu Mar 17, 2011 4:28 pm
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);
?
Re: Needle in a haystack
Posted: Thu Mar 17, 2011 4:51 pm
by spacebiscuit
So the replaced text is contained in the $count variable?
Am I reading the code correctly?
Thanks,
Rob.
Re: Needle in a haystack
Posted: Thu Mar 17, 2011 7:22 pm
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).
Re: Needle in a haystack
Posted: Thu Mar 17, 2011 7:43 pm
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.
Re: Needle in a haystack
Posted: Thu Mar 17, 2011 7:50 pm
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).
Re: Needle in a haystack
Posted: Thu Mar 17, 2011 8:06 pm
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.
Re: Needle in a haystack
Posted: Thu Mar 17, 2011 8:53 pm
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.
Re: Needle in a haystack
Posted: Thu Mar 17, 2011 9:03 pm
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.
Re: Needle in a haystack
Posted: Thu Mar 17, 2011 9:55 pm
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);
Re: Needle in a haystack
Posted: Fri Mar 18, 2011 4:39 am
by Weirdan
Jonah Bron wrote:You mean you tried this?
$replace[$i] wouldn't make sense in your code since $replace is a string.
Re: Needle in a haystack
Posted: Fri Mar 18, 2011 10:34 am
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:
It really does not work.
Rob.
Re: Needle in a haystack
Posted: Fri Mar 18, 2011 10:39 am
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.
Re: Needle in a haystack
Posted: Fri Mar 18, 2011 10:43 am
by spacebiscuit
So why when I output the replaced array variable are three text boxes being drawn?
Re: Needle in a haystack
Posted: Fri Mar 18, 2011 1:09 pm
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?