PHP匹配2个字符串

时间:2018-08-21 14:26:01

标签: php string match

我有2个字符串要比较。

$base_string =  "job, jobs, hiring, for hire, hire";
$user_string = "virtual assistant jobs";

if($user_string matches $base_string){
    return true; //true for jobs word
}else{
    return false;
}

根据上面的伪代码,您能帮我如何编码以上逻辑吗?

2 个答案:

答案 0 :(得分:2)

$base_string =  "job, jobs, hiring, for hire, hire";
$user_string = "virtual assistant jobs";

$array_match = explode(",",$base_string);
$array_search = explode(" ",$user_string);

if(sizeof(array_intersect($array_match,$array_search)) > 0){
    return true; //true for jobs word
}else{
    return false;
}

答案 1 :(得分:0)

您还可以执行以下操作

<?php

$base_string =  "job, jobs, hiring, for hire, hire";
$user_string = "virtual assistant jobs";

$user_string = explode(" ", $user_string); //store the search string in a array to loop through with strpos

foreach ($user_string as $string_value){

  $string = $string_value;
  if (strpos($base_string, $string) !== false) {
        echo 'true';
  } else {
        echo 'false';
  }
}