exact word matching

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boyindie86
    New Member
    • Jun 2007
    • 31

    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($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
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    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($wo rd, $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

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      Originally posted by nathj
      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($wo rd, $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
      Code:
      This is a nice car."
      "Car xx is better then mine"
      No, you'll have to stick with a regular expression.

      Ronald

      Comment

      • nathj
        Recognized Expert Contributor
        • May 2007
        • 937

        #4
        Originally posted by ronverdonk
        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($wo rd, $replace, $passage) ;
        ?>
        [/php]


        That should, I think, handle the original query.

        Cheers
        nathj

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          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

          Comment

          • nathj
            Recognized Expert Contributor
            • May 2007
            • 937

            #6
            Originally posted by ronverdonk
            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

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              Ok, I understand. And I agree that a regular expression is the ultimate solution.

              Ronald

              Comment

              Working...