Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/174103-differences-between-2-strings/
Share on other sites

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 :D)

 

$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!

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

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);

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

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.

$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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.