这是我的例子:
$string1 = "I love php coding and want to get some help";
$string2 = "I like java coding and like to help other people" ;
$output = anyfunction($string1, $string2);
我希望得到输出= 我编码和帮助
php中是否有内置函数来执行此任务?
答案 0 :(得分:1)
试试这个: -
<?php
$string1 = "I love php coding and want to get some help";
$string2 = "I like java coding and like to help other people" ;
function getCommonCharacter($str1,$str2,$case_sensitive = false)
{
$ary1 = explode(' ',$str1);
$ary2 = explode(' ',$str2);
if ($case_sensitive)
{
$ary1 = array_map('strtolower',$ary1);
$ary2 = array_map('strtolower',$ary2);
}
echo (implode(' ',array_intersect($ary1,$ary2)));
}
getCommonCharacter($string1,$string2,$case_sensitive = false);
?>
输出: - http://prntscr.com/746ewy
注意: - 区分大小写为false,以匹配小写或大写。你可以改变以查看其他结果。谢谢。