When you wanted to filter strings through words, you can use array diff to filter known words on string.
Example :
Words = “I want to eat something this morning, but I don’t have any money”
Filter Words = “this”, “but”, “any”
In Php you can use the array diff, first we will separated the words by making it into arrays ..
1 2 |
$textsplit = "I want to eat something this morning, but I don't have any money"; $textsplit = explode(" ", $text); |
After the words become array, we then add the filter words into another array and use the array diff to seperate them ..
1 2 |
$textarr = array("this", "but", "any"); $textsplit = array_diff($textsplit, $textarr); |
The value in $textsplit is still in array format, so we use the implode function to stick them together into a string ..
1 |
$text = implode(" ", $textsplit); |
The final coding will look like this ..
1 2 3 4 5 6 7 8 |
<?php $textsplit = "I want to eat something this morning, but I don't have any money"; $textsplit = explode(" ", $text); $textarr = array("this", "but", "any"); $textsplit = array_diff($textsplit, $textarr); $text = implode(" ", $textsplit); echo $text; ?> |
The Output will be
1 |
I want to eat something morning, I don't have money |