首先,使用
str_split()
检查每个字符。
//convert string into array
$strArr = str_split('AGHIJKLAL');
$temp = []; //temporary array
foreach($strArr as $v){
//check each character if it is in temp array or not, if yes, character matched and exit from loop using break;
if(in_array($v, $temp)){
$repeatChar = $v;
break;
}else{ // if not matched store character into temp array.
$temp[] = $v;
}
}
echo $repeatChar;
Demo