RE: [PHP] determine which string is longer and then what is different in that string

2007-08-26 Thread Sanjeev N
Hi Richard,

I am trying to get your actual problem.
First, you want to compare 2 strings and want to check what the difference
between these 2 strings is. I assuming string may contain either numbers or
any characters.

Second, you are trying to compare the numbers.

But I am thinking that your problem is first case.
Then first you check the length, and then check character by character
(check each position, if different character at same position means changed)
for each string.

Warm Regards,
Sanjeev
http://www.sanchanworld.com/
http://webdirectory.sanchanworld.com - Submit your website URL
http://webhosting.sanchanworld.com - Choose your best web hosting plan

-Original Message-
From: Richard Kurth [mailto:[EMAIL PROTECTED] 
Sent: Sunday, August 26, 2007 11:41 AM
To: php-general@lists.php.net
Subject: [PHP] determine which string is longer and then what is different
in that string

I am trying to find out which string is the longest and then find out what
is different between the two string. Sometimes String 2 is longer than
String 1 The script below works sometimes but not all the time.
Is there a better way to do this or tell me what is wrong with the script I
am using
 
$string1 = 1,2,3,4,5,6,7,8;
$string2 = 1,2,3,4,6,7,8; 
 
 
 
function compare_by_length ($a, $b)
{
$la = strlen ($a);
$lb = strlen ($b);
if ($la == $lb) return (0);
return ($a  $b) ? 1 : 2;
}
 
$string1 = 1,2,3,4,5,6,7,8;
$string2 = 1,2,3,4,6,7,8; 
 
$longstring=compare_by_length ($string1, $string2);
 
if ($longstring==0){
  echo nochange . $longstring;
  echobr;
}
if ($longstring==1){
  echo List  . $longstring;
  echobr;
  $array1 = explode(,, $string1);
  $array2 = explode(,, $string2);
}
if ($longstring==2){
  echo List  . $longstring;
  echobr;
  $array1 = explode(,, $string2);
  $array2 = explode(,, $string1);
 
}
 
   for ($i = 0; $i  count($array1); $i++) {
  $associative_array[$array1[$i]] = 1;
   }
   
   // print Not a member of the following lists:br\n;
   for ($i = 0; $i  count($array2); $i++) {
  if (!$associative_array[$array2[$i]]) {
 print $array2[$i]br\n;
  }
   }

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] determine which string is longer and then what is different in that string

2007-08-26 Thread Richard Lynch


On Sun, August 26, 2007 1:11 am, Richard Kurth wrote:
 I am trying to find out which string is the longest and then find out
 what
 is different between the two string. Sometimes String 2 is longer than
 String 1 The script below works sometimes but not all the time.
 Is there a better way to do this or tell me what is wrong with the
 script I
 am using

 $string1 = 1,2,3,4,5,6,7,8;
 $string2 = 1,2,3,4,6,7,8;

I didn't follow your code, but I think your answer lies in here
somewhere:

$array1 = explode(',', $string1);
$array2 = explode(',', $string2);
$diff1 = array_diff($array1, $array2);
$diff2 = array_diff($array2, $array1);
$diff = array_merge($diff1, $diff2);
var_dump($diff1);
var_dump($diff2);
var_dump($diff);

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php