php搜索字符串逗号分隔并获取匹配的元素

I have a question, if anyone can help me to solve this. I have a string separated by commas, and I want to find an item that partially matches:

$search = "PrintOrder";
$string = "IDperson, Inscription, GenomaPrintOrder, GenomaPrintView";

I need to get only the full string from partial match as a result of filter:

$result = "GenomaPrintOrder";

With preg_match_all you can do like this.

Php Code

<?php
  $subject = "IDperson, Inscription, GenomaPrintOrder, GenomaPrintView, NewPrintOrder";
  $pattern = '/\b([^,]*PrintOrder[^,]*)\b/';
  preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
  foreach ($matches as $val) {
      echo "Matched: " . $val[1]. "
";
  }
?>

Output

Matched: GenomaPrintOrder
Matched: NewPrintOrder

Ideone Demo

$search = "PrintOrder";
$string = "IDperson, Inscription, GenomaPrintOrder, GenomaPrintView";
$result = array();
$tmp = explode(",", $string);
foreach($tmp as $entrie){
    if(strpos($entrie, $string) !== false)
        $result[] = trim($entrie);
}

This will get you an array with all strings that match your search-string.

You can use regular expression to get the result:

$search = "PrintOrder";
$string = "IDperson, Inscription, GenomaPrintOrder, GenomaPrintView";

$regex = '/([^,]*' . preg_quote($search, '/') . '[^,]*)/';

preg_match($regex, $string, $match);

$result = trim($match[1]); // $result == 'GenomaPrintOrder'
$search = "PrintOrder";
$string = "IDperson, Inscription, GenomaPrintOrder, GenomaPrintView";


$array = explode(" ", $string);
echo array_filter($array, function($var) use ($search) { return preg_match("/\b$searchword\b/i", $var); });

Since there are so many different answers already, here is another:

$result = preg_grep("/$search/", explode(", ", $string));
print_r($result);