Home  >  Article  >  Backend Development  >  关于字符交集方法

关于字符交集方法

WBOY
WBOYOriginal
2016-06-23 14:01:131437browse

目前有2个字符串
"A B C"
"D E F"
判断字符串总的字符是否有交集,我自己写了个方法,觉得有点复杂,是将字符转为数组比较,有没有直接比较的函数?二是字符之间是用空格分开的,如果没有空格,有没有更快的方法将字符串转为数组。。


$str_a="A B C";
$str_b="D E F";
$R=array_intersect(explode(" ",$str_a),explode(" ",$str_b));
echo !empty($R)?"有交集":"无交集";


回复讨论(解决方案)

空格也是字符呀,为何不算?

无空格时

$str_a = "ABC";$str_b = "DEF";echo (similar_text($str_a, $str_b) ? '有' : '无') . '交集'; //无交集$str_a = "ABC";$str_b = "DEB";echo (similar_text($str_a, $str_b) ? '有' : '无') . '交集'; //有交集

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn