How to remove the words from string that exist in another string with PHP

1 Answer

0 votes
$s = "How to extract all digits from string in PHP Collective Solver";
$search = "CollectiveSolver";

$arr = explode(" ", $s);

for ($i = 0; $i < count($arr) + 1; $i++) {
  if (strpos($search, $arr[$i]) !== false)
      unset($arr[$i]);
}

print_r($arr);



/*
run:

Array
(
    [0] => How
    [1] => to
    [2] => extract
    [3] => all
    [4] => digits
    [5] => from
    [6] => string
    [7] => in
    [8] => PHP
)

*/

 



answered Dec 19, 2020 by avibootz
...