473,382 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

exact word matching

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
6 3344
nathj
938 Expert 512MB
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
4,258 Expert 4TB
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
938 Expert 512MB
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
4,258 Expert 4TB
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
938 Expert 512MB
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
4,258 Expert 4TB
Ok, I understand. And I agree that a regular expression is the ultimate solution.

Ronald
Feb 25 '08 #7

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

Similar topics

2
by: Juggernaut | last post by:
Hi I was trying to write a script to replace some text inside some tags. Lets say I had <tag stuff=stuff><tag stuff=otherstuff><another> I wanted it to find all the <tag and remove them. So...
3
by: Ian Gil | last post by:
I'm looking to match the 3rd (or whatever) word in a line, where word can be anything that's not white space. I'm not programming, but I'm using a program which says it uses python style regular...
6
by: Mark Findlay | last post by:
I am trying to figure out how to set up my reg exp search so that the search will only match on the exact word. Here is the current problem code: Word1 = "RealPlayer.exe" Word2 = "Player.exe"...
0
by: David Akerman | last post by:
Hi, Background ======== I've written a Web App in C# which controls a pool of Word Applications for mailmerge purposes (not ideal using Word non-interactively I know, but I have explored...
2
by: Jon Lapham | last post by:
I have a table that stores TEXT information. I need query this table to find *exact* matches to the TEXT... no regular expressions, no LIKE queries, etc. The TEXT could be from 1 to 10000+...
4
by: jmdaviault | last post by:
I want to do the equivalent of SELECT id from TABLE WHERE text='text' only fast solution I found is: SELECT id,text from TABLE WHERE MATCH(text) AGAINST('value' IN BOOLEAN MODE) HAVING...
2
by: kamalatanvi | last post by:
hi, i am very new to perl how to give exact pattern matching example: i have the data as follows mm100 mm100-m1 mm101
0
by: saravanakumar muthurangan | last post by:
Hello all, i need to correct a misspelled word automatically with a most matching word by using MS word.dll in vb.net 2005, i m getting the checkspelling window with the below code but...
12
by: Archanak | last post by:
Hi, I have a word like this: "Rna binding proteins" and i want to match this exact phrase. I have written code like this: $sentence="Overall, the participation of additional RNA binding...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.