I can’t do a word search between brackets in php

Asked

Viewed 247 times

3

I created a function to check if some words exist in an array. I want to search for words within "[ ] brackets". Example: [how] [to give], [to love] ...

For this, I am using the function preg_match() to check if there are any of the words in the array. However, the function is accepting word checking without brackets.

Example: The word "Darkgreen" is accepted because it has "give" at first, but I just want you to accept "[give]"

This is my code and an example of text where I check:

My code:

private function existeAlgum($post){
    $saida = array();

    foreach ($this->termos as $termo) {
        $er = "[".$termo."]";
        if(preg_match($er,$post)){
            $saida[] = $termo;
        }
    }

    return $saida;
}

Array example:

 <dl>
    <dt><b><font color="maroon">como</font></b> 

    <font color="maroon">[como]</font>  &lt;rel&gt; &lt;ks&gt; <font color="blue"><b>ADV</b> </font> <font color="darkgreen">@ADVL&gt;</font> <font color="darkgreen"><b>@#FS-ADVL</font></b> <font color="darkgreen"><b>@#FS-N&lt;</font></b>
    <dt><b><font color="maroon">não</font></b> 

    <font color="maroon">[não]</font>  <font color="blue"><b>ADV</b> </font> <font color="darkgreen">@ADVL&gt;</font>
    <dt><b><font color="maroon">amar</font></b> 

    <font color="maroon">[amar]</font>  &lt;vt&gt; <font color="blue"><b>V</b> FUT 1/3S SUBJ VFIN </font> <font color="darkgreen">@FMV</font>
    <dt><b><font color="maroon">uma</font></b> 

    <font color="maroon">[um]</font>  &lt;arti&gt; <font color="blue"><b>DET</b> F S </font> <font color="darkgreen">@&gt;N</font>
    <dt><b><font color="maroon">pessoa</font></b> 

    <font color="maroon">[pessoa]</font>  &lt;H&gt; <font color="blue"><b>N</b> F S </font> <font color="darkgreen">@&lt;ACC</font>
    <dt><b><font color="maroon">tão</font></b> 

    <font color="maroon">[tão]</font>  &lt;dem&gt; &lt;quant&gt; <font color="blue"><b>ADV</b> </font> <font color="darkgreen">@&gt;A</font>
    <dt><b><font color="maroon">linda</font></b> 

    <font color="maroon">[lindo]</font>  <font color="blue"><b>ADJ</b> F S </font> <font color="darkgreen">@N&lt;</font>
    <dt><b><font color="maroon">.</font></b> 

    </dl>
  • Related en.stackoverflow.com/questions/81863/expression-regular-to-get-what-is-out-of-brackets/81896

  • What you have in the variable $this->termos?

  • The $this->terms contain the words: how to, give, love....

1 answer

5


You are wearing clasps.
They are elements of regular squeeze syntax.

In addition, the function preg_match needs an initial delimiter that in this case can be the hashtag #. And to accept the bracket as part of the string, use the backslash as the character escape.

Thus remaining:

private function existeAlgum($post){
    $saida = array();

    foreach ($this->termos as $termo) {
        $er = "#\\[".$termo."\\]#";
        if(preg_match($er,$post)){
            $saida[] = $termo;
        }
    }

    return $saida;
}
  • 1

    vlw guy worked =D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.