Find words in a string

1

I'm creating a function in PHP that gets a string . Then it should search within that string words within brackets and save them in an array.

Is there a function that performs the search for just those words and save them to an array? Or in a variable for later I passed it to an array?

String example:

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

array example

Array
(
    [0] => como
    [1] => não
    [2] => amar
    [3] => um
    [5] => pessoa
    [6] => tão
    [7] => lindo

)
    
asked by anonymous 20.09.2015 / 17:44

1 answer

3

You can use a regular expression, to capture only the characters between the brackets, the only detail is to escape them since they are metacharacters.

The expression is \[.*\]/ , it means capture one opens brackets followed by any character the largest number of times and must stop when encountering a date brackets.

<?php
$str = '
<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>';

$regex ='/\[.*\]/';
preg_match_all($regex, $str, $matches);

echo '<pre>';
print_r($matches);

Example - ideone

    
20.09.2015 / 17:56