I have this php code
$filename = "verbs.php"; // http://alylores.x10.mx/vega/verbs2.php
$handle = fopen($filename, "r");
$verbs = fread($handle, filesize($filename));
fclose($handle);
and i used PHP explode()
function
to split the words into array
$verbslist = explode(",", $verbs);
and i also have a string, like:
$sentence = "Where is Phisz' dog?";
and then i used the str_replace()
function to remove the verbs and some specifice words from the sentence, so that the only left will be the subject(s).
$newsentence = str_replace($verbslist,"",$sentence);
but the result is:
new Sentence: Phz' dog?
// the is
on Phisz
was also removed.
and i figured out that the problem is that the Phisz
words contain is
which was also removed with the str_replace()
.
what i want is how can i remove the words/vebs from the sentence without affecting other words. I mean removing the EXACT VERB/WORD..... and in case insensitive...
that the expected result will be like this
new Sentence: Phisz' dog?
Using a regular expression like /\bword\b/
will replace only the word as a whole. \b
denotes a word boundary. So you can do something like this:
foreach ($verblist as &$verb) {
$verb = '/\b' . preg_quote($verb, '/') . '\b/';
}
$newsentence = preg_replace($verblist, '', $sentence);
Since you want to remove exact verbs you can put spaces around each of your verbs so your list would be something like this with _ denoting a space
" is "
" where "
etc.
Then to get case insensitive make all your verbs lower case and then wrap your verbs in the strtolower()
strtolower($sentence)
So your replace would look something like this:
$newsentence = str_replace($verbslist,"",strtolower($sentence));