开发者

Create array out of string and then compare with another array

I have two questions: 1. How to break a string into an array given a start string and end string make array element out of everything inbetween. 2. Compare my new array with another array and if any elements match let the user know which values match.

I need to break the contents of a string into an array I need the values of the mac address

[MAC]
mac=12EG23OCSOPC
mac=111111111111
mac=222222222222
mac=333333333333

I need the values of everything after "mac=" and before the \n

and then I need to compare these values with another array, if there are items that are the same i need to let the user know which ones match This is what I have so far.

function count_compare_macs($macs_to_compare, $new_array)

{
    $macs_to_compare_count = count($macs_to_compare);
    $new_array_count = count($new_array);

        if(($macs_to_compare_count + $new_array_count) >= 5)
            {

            return  false;
                    $error_message="You already have 5 MAC address in your system";
            }else{
              //here is where i need to compare the two arrays and if any are the same say false and set the error message.
                      $compare_arrays = array_Diff($macs_to_compare,$new_array)
                         if (!$compare_arrays){
                                return true;
                               }else{


//here is where i need to compa开发者_JAVA技巧re the two arrays and if any are the same say false and set the error message.
                                      $error_message= "The following mac is already used" . $match
return false;
                                       }
                }
    }


what you can do for #1 is:

$str = "mac=12EG23OCSOPC
mac=111111111111
mac=222222222222
mac=333333333333";

$strSplode = explode("\n",$str);

$mac = array();

foreach($strSplode as $m){
     list($temp, $mac[]) = explode("=", $m);
}

here is a demo of part 1: http://codepad.org/Taiyrvf2
(in the demo i had to explode the 1st part by \r\n because that is what the lines were split by, it all depends on how the string was encoded)

for the 2nd part:

function find_matches($mac1, $mac2){

   $matches = array();

   foreach($mac1 as $mac){
      if(in_array($mac, $mac2)){
          $matches[] = $mac;
      }
   }

   //return an array of matching mac addresses
   return $matches;

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜