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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 6:00 PM -0700 8/26/07, 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";
 >
 >
 Can you give us an example of two strings where the comparison fails?
 I suspect you are looking for the number of "entries" in the
 string, rather than the length of the string. For example:
 $string1 = "1,2,3,4,5,6,9";
 $string2 = "1,2,3,10,11,12";

 number of entries in $string1 is 7, number of entries in
 $string2 is 6, but length($string1) = 13, while
 length($string2) = 14. Is this the problem you are seeing?

Yes that is the problem $string2 is what is saved in the database and
string1 is coming from a form. The form has every thing that is in the
database but could have some items added and some removed. I need to know
what is different so I can add to or remove from a separate table so both
tables are the same.

How about something like

$array_string1 = explode(',' , $string1);
$array_string2 = explode(',' , $string2);
$len_string1 = count($array_string1);
$len_string2 = count($array_string2);

Now you can compare the number of elements in $string1 ($len_string1) vs. the number of elements in $string2 ($len_string2). And the two arrays can be compared to see what the differences are.

You haven't really provided enough information about what is really going on (nor is it necessary to do so), but is it possible that the two strings could be different but have the same number of elements? If so, then the lenght of the strings is not particularly relevant, and you might just want to compare the specific elements of both strings with each other.

     -----===== Bill =====-----
--

If you're living on the edge, make sure you're wearing your seat belt.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux