<?php
function token($word, array $listconjunctions=['and', 'on', 'in', 'or', 'which'])
{
return str_replace($listconjunctions,'',$word);
}
function token1($word, array $listconjunctions=['and', 'on', 'in', 'or', 'which'])
{
$listconjunctions = array_map('preg_quote', $listconjunctions);
$pattern = [
'~\b('.implode('|',$listconjunctions).')\b~i', //words
'~\s+~', //run on spaces
'~\s+([^\w]+)$~' //spaces before punctuation, at the end of line
];
return preg_replace($pattern, [' ', ' ', '$1'], $word);
}
$string = "this that and on and on the other which.";
echo token($string)."\n\n";
echo token1($string)."\n\n";
//second deveation
echo token1('this that and, another one.');
this that the other .
this that the other.
this that , another one.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
this that the other .
this that the other.
this that , another one.