Filter linking words from phrase elements

Asked

Viewed 198 times

1

I’m trying to make a word filter in a string, to store in a variable.

Grammatically, they are called prepositions, circumpositions and postpositions.

They are used in binding elements of a sentence.

EX:

$string = "o rato roeu a roupa do rei de roma"  
//aqui a função armazenando as palavras filtradas, que eu acredito que seja um array  
$novoValor = "rato roeu roupa rei roma;  
echo $novovalor; 
  • 1

    It’s to remove which words?

  • The words that link, for example: "from, in, and, a, ", among others less important, but which I will complete with time.

1 answer

1


Utilize in_array to see if the word you want to take exists.

$string = "o rato roeu a roupa do rei de roma"  ;

$frase = explode(' ',$string);
$retirar = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U", "de", "do");

for($x=0;$x<count($frase);$x++){
    if(!in_array($frase[$x], $retirar)){ 
        $novovalor .= $frase[$x].' ';
    }
}

echo $novovalor;
  • A question: will the array delete from within words as well? Causing "test" to "tst" if I add the letter 'E' in the array?

  • I tested it here and it worked perfectly, now just I adapt the variables and add the words to the filter! Before displaying the new value with echo, the following "error" is displayed: Notice: Undefined variable: novovalor in C: wamp www terreiro teste.php on line 9 Line 9: $novovalor .= $phrase[$x].' ';

  • 1

    declare the variable before, getting empty.

  • Perfect! Thank you, really ;)

Browser other questions tagged

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