Reputation: 4055
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 compare 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;
}
}
}
Upvotes: 0
Views: 115
Reputation: 146300
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;
}
Upvotes: 1