Finding substring in string

iau 0 Tallied Votes 241 Views Share

I wish it show as
unfulfilled:bb,cc
extra provided:dd
as my conditions and provided strings will be unconstant, anyone can suggest me some ways to do it?

<?php
$condition="aa,bb,cc";
$provided="aa,dd";
$arr1=explode(",",$condition);
$arr2=explode(",",$provided);
$b=0;
$c=0;

//check sizes of arrays
if(count($arr1)>count($arr2))
{
    $size=count($arr1);
    $a=count($arr2);
}
else
{
    $size=count($arr2);
    $a=count($arr1);
}

//compare both arrays
for($i=0;$i<$size;$i=$i+1)
{
    for($j=0;$j<$a;$j=$j+1)
    {
        if($arr1[$i]==$arr2[$j])
        {
            $num=$num+1;                //check number of matched
            $arr3[$b]=$arr1[$i];        //assign matched to new array
            $b=$b+1;
        }
        
    }
}
echo $num.'matched: ';
while($c<$b)
{
    echo $arr3[$c]." ";
    $c=$c+1;
}
echo "<br>";

?>
Member Avatar for diafol
diafol

Try this:

$condition= "aa,bb,cc"; 
$provided= "aa,dd"; 
$array1 = explode(",",$condition);
$array2 = explode(",",$provided);
echo "Unfilled: " . implode(",",array_udiff($array1,$array2, "strcasecmp"));
echo "<br />";
echo "Extra Provided: " . implode(",",array_udiff($array2, $array1, "strcasecmp"));

/*
Unfilled: bb,cc
Extra Provided: dd
*/
iau 0 Newbie Poster

done, many thanks

Member Avatar for diafol
diafol

OK, mark as solved then.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.