Forum Moderators: coopster
There is a tougher challenge I face involving repeating text, and I hope someone has a clever solution to this. How does one detect repeating strings of arbitrary length?
Let's say a user submits the following: "I am bored I am bored I am bored ..." with dozens of repetitions. I need to programmatically detect this.
But I can't think of any efficient way.
$input = "I am bored I am bored";
$words = split(" ",$input);
$pattern=array_shift($words);
$temp=$pattern;
while(sizeof($words)>0 && !$no_pattern){
if(preg_match_all("/".$pattern."/",$input,$hold) < 2 ) {
$no_pattern=true;
}else{
$temp=$pattern;
$pattern.=' '.array_shift($words);
}
}
$pattern=$temp;
echo "Repeated text is: ".$pattern;
I believe its a similar idea to what darrenG said
EDIT: not sure why my tabs are not showing up, hopefully you can still read the code ok.
also, this assumes that the pattern is at the begining of the string.
[edited by: Wolf_man at 3:49 pm (utc) on May 9, 2008]
Further you cannot figure if a given sentence is made of 3 or 10 or whatever # of words.
So you need to look for a "pattern mode search"
pretty much like a search engine!