By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,216 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,216 IT Pros & Developers. It's quick & easy.

exact word matching

P: 31
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($array,$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("#<textarea(.*?)>(.*?)</textarea>#is",$login->page_html,$content))
{
for($word_count; $word_count<$count_array;$word_count++)
{echo "LOOP1=$word_count<br>";
echo $array[$word_count];
$content[2]=preg_replace("#$array[$word_count]#is","~$array[$word_count]~",$content[2]);
$this->text=$content[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=$this->count<br>";
if ($char_buff[$this->count]=="~")
{
$this->start_word = $this->count;
$a=$a+1;//echo $word_count;
$this->markFound($char_buff,$counter,$text);
$this->text=preg_replace("#~#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<br>";
if($char_buff[$i]=="~")continue;
{
if(preg_match('#[\[\].,?!()";:*@-\s]#',$char_buff[$i+1]))
{
$end_word=$i;
$this->text{$this->start_word-1}="[";
$this->text{$end_word-1 }="]";
$this->count= $i;
$i = 0;
break ;
}
else
{
$j=$i;
for($j; $j<=$counter; $j++)
{echo "LOOP4=$j<br>";
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<br>";
if($char_buff[$j]=="~")
{
$i=$j;
$j=$counter;
}
}
}

}

[/PHP]

Any help would be greatly appreciated

Cheers
boyindie
Feb 22 '08 #1
Share this Question
Share on Google+
6 Replies


nathj
Expert 100+
P: 938
Hi,

first my apologies as I haven't read all the code - it's Friday and I'm feeling a it lazy, so if I missed the point I'm sorry.

Assuming that both pieces of text - the word and the passage are both variables so why not try something like this:

[php]
<?php
$passage = " long text about how to care for a car" ;
$word = " car " ; // note the extra spaces

$finalpassage = str_replace($word, $passage) ;
?>
[/php]

This should do what you want I hope. take a look at str_replace - you can even get a count on the number of replacements made.

Cheers
nathj
Feb 22 '08 #2

ronverdonk
Expert 2.5K+
P: 4,258
Hi,

first my apologies as I haven't read all the code - it's Friday and I'm feeling a it lazy, so if I missed the point I'm sorry.

Assuming that both pieces of text - the word and the passage are both variables so why not try something like this:

[php]
<?php
$passage = " long text about how to care for a car" ;
$word = " car " ; // note the extra spaces

$finalpassage = str_replace($word, $passage) ;
?>
[/php]

This should do what you want I hope. take a look at str_replace - you can even get a count on the number of replacements made.

Cheers
nathj
That cannot work. The str_replace is coded like
[php]mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
[/php]
you'll have to specify $search, $replace and $subject, no defaults.

Maybe you wanted to use strpos. But that would also not work here, because you are searching for " car " (blank char left and right). But this search would not give any results for sentences like
Expand|Select|Wrap|Line Numbers
  1. This is a nice car."
  2. "Car xx is better then mine"
No, you'll have to stick with a regular expression.

Ronald
Feb 22 '08 #3

nathj
Expert 100+
P: 938
That cannot work. The str_replace is coded like
[php]mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
[/php]
you'll have to specify $search, $replace and $subject, no defaults.
Sorry that's correct, I messed out the parameter - I was typing
tired.
However the following should work:
[php]

<?php
$passage = " long text about how to care for a car" ;
$word = " car " ; // note the extra spaces
$replace = " [car] " ;

$finalpassage = str_replace($word, $replace, $passage) ;
?>
[/php]


That should, I think, handle the original query.

Cheers
nathj
Feb 24 '08 #4

ronverdonk
Expert 2.5K+
P: 4,258
Have you tried that sample? It does not work because you are searching for 'car' prefixed and suffixed with a blank. The car in your sentence does not have a blank char as suffix, just as prefix. So it won't work.

Ronald
Feb 24 '08 #5

nathj
Expert 100+
P: 938
Have you tried that sample? It does not work because you are searching for 'car' prefixed and suffixed with a blank. The car in your sentence does not have a blank char as suffix, just as prefix. So it won't work.

Ronald
Ronald,

My code snippet was not intended to be an ultimate solution - I admit it has this flaw and few others. All I was attempting to do was encourage the original poster to think about simpler solutions.

However, the basic premise will catch most cases and could be used as part of an overall solution. The ultimate solution, would I agree, be to use regular expression matching.

Finally, my apologies to the original poster if i have made this more complicated than it needed to be - I was simply trying to illustrate that sometimes there is a simple solution, or a solution made up of simple steps.

Cheers
nathj
Feb 25 '08 #6

ronverdonk
Expert 2.5K+
P: 4,258
Ok, I understand. And I agree that a regular expression is the ultimate solution.

Ronald
Feb 25 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.