[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Compare two strings and display differences

Posted on 2011-10-13
11
Medium Priority
?
256 Views
Last Modified: 2012-08-13
Hi Experts!


I've got two strings like so:
$str1="4211(11)-27(19)-999(43)";
$str2="100(04)-4211(12)-999(43)";
And I want to compare each number from them and display differences.
The output would look like so:

Matching numbers:
999(43)

Numbers exist only in string 1:
4211(11)
27(19)

Numbers exist only in string 2:
100(04)
4211(12)


Thanks for any help.
0
Comment
Question by:Zado
  • 5
  • 4
  • 2
11 Comments
 
LVL 7

Accepted Solution

by:
amigura earned 1200 total points
ID: 36961715
$str1="4211(11)-27(19)-999(43)";
$str2="100(04)-4211(12)-999(43)";

$str1_array=explode('-',$str1);
$str2_array=explode('-',$str2);


$str_exist1 = array_diff($str1_array, $str2_array);

print_r($str_exist1);

$str_exist2 = array_diff($str2_array, $str1_array);

print_r($str_exist2);

Open in new window

0
 
LVL 8

Author Comment

by:Zado
ID: 36961748
Thanks amigura.

1.How to display matching numbers?
2.How to convert array format into text format like this?:
Matching numbers:
999(43)

Numbers exist only in string 1:
4211(11)
27(19)

Numbers exist only in string 2:
100(04)
4211(12)
...'foreach' function?
0
 
LVL 7

Expert Comment

by:amigura
ID: 36961753
matches


$matches_1_2 = array_intersect($str1_array, $str2_array);
print_r($matches_1_2);

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:amigura
ID: 36961770
you can use implode

http://uk.php.net/manual/en/function.implode.php


$str_exist1 = implode("\n", $str_exist1);
$str_exist2 = implode("\n", $str_exist2);
$matches_1_2 = implode("\n", matches_1_2);

Open in new window

0
 
LVL 8

Author Comment

by:Zado
ID: 36961834

This is great! Just one small thing, third line doesn't work (with one or more variables):
$matches_1_2 = implode("\n", matches_1_2);

Open in new window

I don't know where's the problem, syntax is ok.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36961940
Are the values always separated by the hyphens?
0
 
LVL 8

Author Comment

by:Zado
ID: 36961966
Yes, they are.
0
 
LVL 7

Expert Comment

by:amigura
ID: 36962000
its missing the $ on matches

$matches_1_2 = implode("\n", $matches_1_2);

Open in new window


Ray shhhhh ;)

yes btw it will only work for any string seperated by - 4211(11)-27(19) and not for 4211(11)/27(19) for example


0
 
LVL 8

Author Comment

by:Zado
ID: 36962043
Oh, so the syntax wasn't ok ;-)
Thanks a lot!
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 total points
ID: 36962052
http://www.laprbass.com/RAY_temp_zado_explode.php
Outputs:
string(23) "4211(11)-27(19)-999(43)"
string(24) "100(04)-4211(12)-999(43)"
THE COMMON ELEMENT(S): 999(43)
UNIQUE TO ARRAY 1: 4211(11),27(19)
UNIQUE TO ARRAY 2: 100(04),4211(12)

Check the man page references in the comments to see the strategy.  Best to all, ~Ray
<?php // RAY_temp_zado_explode.php
error_reporting(E_ALL);
echo "<pre>";


// TEST DATA FROM THE POST AT EE
$str1="4211(11)-27(19)-999(43)";
$str2="100(04)-4211(12)-999(43)";


// SHOW THE ORIGINAL DATA
var_dump($str1);
var_dump($str2);


// CHANGE STRINGS INTO ARRAYS
// http://php.net/manual/en/function.explode.php
$arr1 = explode('-', $str1);
$arr2 = explode('-', $str2);

// FIND THE COMMON ELEMENTS
// http://php.net/manual/en/function.array-intersect.php
$arr = array_intersect($arr1, $arr2);

// http://php.net/manual/en/function.implode.php
$str = implode(',', $arr);

// SHOW THE COMMON ELEMENTS
echo "THE COMMON ELEMENT(S): ";
print_r($str);
echo PHP_EOL;

// FIND THE NON-COMMON ELEMENTS
// http://php.net/manual/en/function.array-diff.php
$dif1 = array_diff($arr1, $arr);
$dif2 = array_diff($arr2, $arr);

// SHOW THE DIFFERENCES FOR EACH ARRAY
$str = implode(',', $dif1);
echo "UNIQUE TO ARRAY 1: ";
print_r($str);
echo PHP_EOL;

$str = implode(',', $dif2);
echo "UNIQUE TO ARRAY 2: ";
print_r($str);
echo PHP_EOL;

Open in new window

0
 
LVL 8

Author Comment

by:Zado
ID: 36962097
Thanks a lot for your help! Works perfectly :-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month18 days, 16 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question