I want to remove all the prepositions(in, at, on...), definite articles(a, an ,the...) and be verbs(is, are, was...) from a article. Is there a short way that php can do that?
$article= "the cup is on the table";// I want get an echo like "cup table"
The key thing is to ensure you only replace whole words (e.g. not the "the" in "there"), for which you can use \b
in PCRE:
$words = array('in', 'at', 'on', 'etc..');
$pattern = '/\b(?:' . join('|', $words) . ')\b/i';
$article = preg_replace($pattern, '', $article);
Caveat 1: If you have a very large number of words to remove, you may hit the maximum pattern size, in which case you can construct the pattern for each one and pass an array of patterns to preg_replace
.
Caveat 2: If the words you want to remove can contain characters of significance to PCRE, then you should call preg_quote($word, '/') to make them safe for the pattern.
str_replace(Array(' in ', ' at ', ' on ', ' a ', ' an ', ' the '), '', $article);
Edit: better, using preg_replace:
$article = 'The cup is on the table.The theble aon is';
echo "<br/>". ($article = preg_replace("/(\W|^)(is|on|a|the)(\W|$)/i", ' ', $article));
echo "<br/>". ($article = preg_replace("/(\W|^)(is|on|a|the)(\W|$)/i", ' ', $article));
Not sure why you have to call the preg_replace
twice though..
Seems like @Lacking Beers has the answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With