Aravinthan Posted September 13, 2009 Share Posted September 13, 2009 Hi, I was wondering how to do a script that finds the difference between 2 strings and outputs it. Exemple: string1 = "This is a test"; string 2 = "This was a testing"; Ouput should be: String 1: is Strng 2: was String 1:test String 2: testing Thanks for your help, Ara Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/ Share on other sites More sharing options...
5kyy8lu3 Posted September 13, 2009 Share Posted September 13, 2009 you could explode(" ", $string1); the string using space then compare the items in your arrays Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/#findComment-917742 Share on other sites More sharing options...
yettti Posted September 13, 2009 Share Posted September 13, 2009 Given that both strings are the same length this would do it... (it even lays it out like you did... but without the spelling mistake ) $string1 = "this is a test"; $string2 = "this was a testing"; $string1 = explode(" ",$string1); $string2 = explode(" ",$string2); foreach ($string1 as $key => $value) { if ($value != $string2[$key]) { echo "String 1 : ".$value; echo "<br>String 2 : ".$string2[$key]."<br><br>"; } } ... little explination, $string1 = explode(" ",$string1); $string2 = explode(" ",$string2); This splits the strings into arrays, it splits them by a space foreach ($string1 as $key => $value) { This then loops through all the items in the array $string1 and assigns the current key and value to variables. if ($value != $string2[$key]) { This checks to see if there not the same... echo "String 1 : ".$value; echo "<br>String 2 : ".$string2[$key]."<br><br>"; and if so prints them out! Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/#findComment-917782 Share on other sites More sharing options...
Aravinthan Posted September 13, 2009 Author Share Posted September 13, 2009 Hi thanks for your help guys. Yetti, thank you but I get this error: Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 2097152 bytes) in /home/liguehs/public_html/converter/test_players.php on line 140014 The 2 strings are pretty big... if I put them in a separate file, they have around 1 500 000 Kilobytes.... Is there a work around? Thanks for your help, Ara Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/#findComment-917791 Share on other sites More sharing options...
yettti Posted September 13, 2009 Share Posted September 13, 2009 Ok.. what your getting there is a error showing that too much memory has been used, because you are reading from a large file... to do it from a large file is a bit different. It can be done... but it would depend on how the file is formated? Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/#findComment-917794 Share on other sites More sharing options...
Aravinthan Posted September 13, 2009 Author Share Posted September 13, 2009 OK I uploaded a file( the whole file is a string ) It was too big to be uploaded on the forums. Link: http://www.megafileupload.com/en/file/134577/players-txt.html Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/#findComment-917798 Share on other sites More sharing options...
yettti Posted September 14, 2009 Share Posted September 14, 2009 Ok, in that case i would advise that you read the file line by line comparing the string on each line, that way you don't end up trying to load the entire file into the variables, instead you load each line, overwriting the last line each loop this... should work, although i haven't tested it $file1 = fopen("doc1.txt", "r"); $file2 = fopen("doc2.txt", "r"); while(!feof($file1 || $file2)) { $string1 = explode(" ",$string1); $string2 = explode(" ",$string2); foreach ($string1 as $key => $value) { if ($value != $string2[$key]) { echo "String 1 : ".$value; echo "<br>String 2 : ".$string2[$key]."<br><br>"; } } } fclose($file); Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/#findComment-918412 Share on other sites More sharing options...
Aravinthan Posted September 14, 2009 Author Share Posted September 14, 2009 Hi, Thanks for your help, But I get the following error: Warning: feof(): supplied argument is not a valid stream resource in /home/liguehs/public_html/converter/test_players.php on line 4 Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/#findComment-918608 Share on other sites More sharing options...
yettti Posted September 15, 2009 Share Posted September 15, 2009 Oh... sorry, my mistake it should be $file1 = fopen("doc1.txt", "r"); $file2 = fopen("doc2.txt", "r"); while(!feof($file1) || !feof($file2))) { $string1 = explode(" ",$string1); $string2 = explode(" ",$string2); foreach ($string1 as $key => $value) { if ($value != $string2[$key]) { echo "String 1 : ".$value; echo "<br>String 2 : ".$string2[$key]."<br><br>"; } } } fclose($file); Made a mistake on the while loop condition. see how i works Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/#findComment-918742 Share on other sites More sharing options...
Zyx Posted September 15, 2009 Share Posted September 15, 2009 It will not work, because you forgot about reading from a file . Anyway, the presented algorithm is too simple and covers only the easiest cases. For example: foo bar joe aaa bbb ccc foo lol bar joe aaa bbb ccc The difference is the extra word, but the algorithm will show that the whole string from this place is different. http://www.xmailserver.org/diff2.pdf Here you have a description of the algorithm used in the Linux diff program. Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/#findComment-918752 Share on other sites More sharing options...
Aravinthan Posted September 15, 2009 Author Share Posted September 15, 2009 Hi I get this error now: Fatal error: Maximum execution time of 20 seconds exceeded in /home/liguehs/public_html/converter/test_players.php on line 6 Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/#findComment-918843 Share on other sites More sharing options...
yettti Posted September 15, 2009 Share Posted September 15, 2009 Cant do much to help you there . This is because you are trying to process a too larger file and the server stops a single script after 20 seconds. you can change this in your php.ini file Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/#findComment-918941 Share on other sites More sharing options...
Aravinthan Posted September 15, 2009 Author Share Posted September 15, 2009 Isnt there a way of giving it a reste? Like mysql rest for exemple? I cant change the php.ini file... my host doesnt allow it. Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/#findComment-918989 Share on other sites More sharing options...
Daniel0 Posted September 15, 2009 Share Posted September 15, 2009 You can use set_time_limit. Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/#findComment-919008 Share on other sites More sharing options...
Aravinthan Posted September 15, 2009 Author Share Posted September 15, 2009 So I put set_time_limit(20) before my script? Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/#findComment-919012 Share on other sites More sharing options...
kingnutter Posted September 15, 2009 Share Posted September 15, 2009 Have a look into array_diff. It might work for you. Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/#findComment-919016 Share on other sites More sharing options...
Aravinthan Posted September 15, 2009 Author Share Posted September 15, 2009 You mean that I explode my files using line breaks, and then I put them in a while loop to run 1 line at a time? Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/#findComment-919017 Share on other sites More sharing options...
Daniel0 Posted September 15, 2009 Share Posted September 15, 2009 So I put set_time_limit(20) before my script? If you read the man page you'll find out. Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/#findComment-919020 Share on other sites More sharing options...
mikesta707 Posted September 15, 2009 Share Posted September 15, 2009 $string1 = "this is a test"; $string2 = "this was a testing"; $string1 = explode(" ",$string1); $string2 = explode(" ",$string2); foreach ($string1 as $key => $value) { if ($value != $string2[$key]) { echo "String 1 : ".$value; echo "<br>String 2 : ".$string2[$key]."<br><br>"; } } unless you are trying to see if a string is exactly the same, this won't work. if you want to take a string, and compare each word and see which words are different, I suggest you do a string explode, and use the array_diff function. As far as your files problems, if you can't change php.ino then you probably want to set a time limit of more than 20, like 30 seconds or something. Quote Link to comment https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/#findComment-919032 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.