Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove some words in php

Tags:

php

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" 
like image 654
cj333 Avatar asked Dec 02 '22 03:12

cj333


2 Answers

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.

like image 107
Long Ears Avatar answered Dec 25 '22 03:12

Long Ears


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.

like image 28
Quamis Avatar answered Dec 25 '22 04:12

Quamis