Hi
I have been fighting with this lump of code for the last week what I am trying to do is that I am passing words into passages of texts, and I want the system to go and find exact word matches only and place square brackets around them e.g
word = car
PASSAGE:
the red car required a lot of care to prevent a scar
I only want it to place a square bracket around the word car and ignore the car in "Care" and "scar"
i can't seem to get a preg_replace which will work exactly due to it can't tell if there is a character before it or after it which would make it a different word
I have been writing a method were I pass the each word through the system one at a time and i place "~" before and after the word as a marker.
I have then been trying to loop through the string trying to find the marker and i check one character before and one character after word, to see if it is a word or punctuation and I then replace the marker with a square bracket. So the result would look like
the red [car] required a lot of care to prevent a scar
my code is as follows I think there has to be a simpler way than doing the following as there is so many scenarios which just cause it not to work
[PHP]public function createLinks($ar ray,$url,$id)
{
$login = new loginManager();
$login->loginSite();
$edit= "$url/edit";
$a="";
$word_count=0;
$login->getPageWrite($ edit);
$count_array = count($array);
//Search page for content tage and read content into text file
if( preg_match("#<t extarea(.*?)>(. *?)</textarea>#is",$ login->page_html,$con tent))
{
for($word_count ; $word_count<$co unt_array;$word _count++)
{echo "LOOP1=$word_co unt<br>";
echo $array[$word_count];
$content[2]=preg_replace(" #$array[$word_count]#is","~$array[$word_count]~",$content[2]);
$this->text=$conten t[2];
$char_buff = preg_split('//', $this->text, -1);
$counter=count( $char_buff);
//$counter = $counter -1;
for($this->count=0; $this->count<$counter ; $this->count++)
{echo "LOOP2=$thi s->count<br>";
if ($char_buff[$this->count]=="~")
{
$this->start_word = $this->count;
$a=$a+1;//echo $word_count;
$this->markFound($cha r_buff,$counter ,$text);
$this->text=preg_repl ace("#~#is","", $this->text);
}
}
}
}
echo $this->text;[/PHP]
[PHP]public function markFound($char _buff,$counter, $text)
{
if(preg_match(' #[][.,?!()";:*@-\s]#',$char_buff[$this->count-1]))
{
$i = $this->count+1;
for($i; $i<=$counter;$i ++)
{echo "LOOP3=$i<b r>";
if($char_buff[$i]=="~")continu e;
{
if(preg_match(' #[\[\].,?!()";:*@-\s]#',$char_buff[$i+1]))
{
$end_word=$i;
$this->text{$this->start_word-1}="[";
$this->text{$end_wo rd-1 }="]";
$this->count= $i;
$i = 0;
break ;
}
else
{
$j=$i;
for($j; $j<=$counter; $j++)
{echo "LOOP4=$j<b r>";
if($char_buff[$j]=="~")
{
$j = $counter;
$i = $counter;
//$i = $j+1;
break ;
}
}
}
}
}
}
else
{
$j = $this->count+1;
for($j ;$j<=$counter; $j++)
{echo "LOOP5=$j<b r>";
if($char_buff[$j]=="~")
{
$i=$j;
$j=$counter;
}
}
}
}
[/PHP]
Any help would be greatly appreciated
Cheers
boyindie
I have been fighting with this lump of code for the last week what I am trying to do is that I am passing words into passages of texts, and I want the system to go and find exact word matches only and place square brackets around them e.g
word = car
PASSAGE:
the red car required a lot of care to prevent a scar
I only want it to place a square bracket around the word car and ignore the car in "Care" and "scar"
i can't seem to get a preg_replace which will work exactly due to it can't tell if there is a character before it or after it which would make it a different word
I have been writing a method were I pass the each word through the system one at a time and i place "~" before and after the word as a marker.
I have then been trying to loop through the string trying to find the marker and i check one character before and one character after word, to see if it is a word or punctuation and I then replace the marker with a square bracket. So the result would look like
the red [car] required a lot of care to prevent a scar
my code is as follows I think there has to be a simpler way than doing the following as there is so many scenarios which just cause it not to work
[PHP]public function createLinks($ar ray,$url,$id)
{
$login = new loginManager();
$login->loginSite();
$edit= "$url/edit";
$a="";
$word_count=0;
$login->getPageWrite($ edit);
$count_array = count($array);
//Search page for content tage and read content into text file
if( preg_match("#<t extarea(.*?)>(. *?)</textarea>#is",$ login->page_html,$con tent))
{
for($word_count ; $word_count<$co unt_array;$word _count++)
{echo "LOOP1=$word_co unt<br>";
echo $array[$word_count];
$content[2]=preg_replace(" #$array[$word_count]#is","~$array[$word_count]~",$content[2]);
$this->text=$conten t[2];
$char_buff = preg_split('//', $this->text, -1);
$counter=count( $char_buff);
//$counter = $counter -1;
for($this->count=0; $this->count<$counter ; $this->count++)
{echo "LOOP2=$thi s->count<br>";
if ($char_buff[$this->count]=="~")
{
$this->start_word = $this->count;
$a=$a+1;//echo $word_count;
$this->markFound($cha r_buff,$counter ,$text);
$this->text=preg_repl ace("#~#is","", $this->text);
}
}
}
}
echo $this->text;[/PHP]
[PHP]public function markFound($char _buff,$counter, $text)
{
if(preg_match(' #[][.,?!()";:*@-\s]#',$char_buff[$this->count-1]))
{
$i = $this->count+1;
for($i; $i<=$counter;$i ++)
{echo "LOOP3=$i<b r>";
if($char_buff[$i]=="~")continu e;
{
if(preg_match(' #[\[\].,?!()";:*@-\s]#',$char_buff[$i+1]))
{
$end_word=$i;
$this->text{$this->start_word-1}="[";
$this->text{$end_wo rd-1 }="]";
$this->count= $i;
$i = 0;
break ;
}
else
{
$j=$i;
for($j; $j<=$counter; $j++)
{echo "LOOP4=$j<b r>";
if($char_buff[$j]=="~")
{
$j = $counter;
$i = $counter;
//$i = $j+1;
break ;
}
}
}
}
}
}
else
{
$j = $this->count+1;
for($j ;$j<=$counter; $j++)
{echo "LOOP5=$j<b r>";
if($char_buff[$j]=="~")
{
$i=$j;
$j=$counter;
}
}
}
}
[/PHP]
Any help would be greatly appreciated
Cheers
boyindie
Comment