[code][?php
$variable1=‘I have two variables.
They are variabe1 and variable2
Variable1 and variable2 are almost same.
Only a few words are different between variable1 and variable2.
The variable has “X” and the other variable has “Y”
This variabe1 is different from the variable2.
while variable1 has “X”, variable2 has “Y”.
The first different word is on the end of the 4th line.
I like to find the first diffent position of the variable1 compared with variable2.’;
$variable2=‘I have two variables.
They are variabe1 and variable2
Variable1 and variable2 are almost same.
Only a few words are different between variable1 and variable2.
The variable has “Y” and the other variable has “X”
This variabe1 is different from the variable2.
while variable1 has “X”, variable2 has “Y”.
The first different word is on the end of the 4th line.
I like to find the first diffent position of the variable1 compared with variable2.’;
echo ‘“X” position of variable1=’.strpos($variable1,‘X’).‘[br]’;
echo ‘“X” position of variable2=’.strpos($variable2,‘X’).‘[hr]’;
echo ‘“Y” position of variable1=’.strpos($variable1,‘Y’).‘[br]’;
echo ‘“Y” position of variable2=’.strpos($variable2,‘Y’).‘[hr]’;
echo ‘“X” position of variable1=’.strpos($variable1,‘X’).‘[br]’;
echo ‘“Y” position of variable2=’.strpos($variable2,‘Y’).‘[hr]’;
?]
[/code]The result of the code above is at http://dot.kr/x-test/php/delimeter_position.php .
“X” position of variable1 “181” means the position of the first different letter compared with variable2.
the 181th position is the 1st different letter between variable1 and variable2.
I like to find the position of the first differnt letter between 2 variables.
How can I find the position of the 1st different letter between 2 variables Regardless of the first different letter is “X”, “Y”, or “Z”?
I couldn’t see a standard function to do that, but it’s a pretty simple function to write, I’d do it something like this:
function find_mismatch($str1, $str2) {
$count = 0;
while ($count < (min(strlen($str1), strlen($str2)))) {
if (substr($str1, $count, 1) != substr($str2, $count, 1)) {
// $count is the first different character
return $count;
} // end of if()
$count += 1;
} // end of while()
return 0;
} // end of function
I haven’t dealt with how you’d return a status to indicate the strings are identical, or that one is shorter than the other (and which one), or whether you’d just ignore that fact and only compare until the shortest one runs out.
I’m sure there must already be something that does this, though.
<?php
$str1='I have two variables.
They are variabe1 and variable2
Variable1 and variable2 are almost same.
Only a few words are different between variable1 and variable2.
The variable has "X" and the other variable has "Y"
This variabe1 is different from the variable2.
while variable1 has "X", variable2 has "Y".
The first different word is on the end of the 4th line.
I like to find the first diffent position of the variable1 compared with variable2.';
$str2='I have two variables.
They are variabe1 and variable2
Variable1 and variable2 are almost same.
Only a few words are different between variable1 and variable2.
The variable has "Y" and the other variable has "X"
This variabe1 is different from the variable2.
while variable1 has "X", variable2 has "Y".
The first different word is on the end of the 4th line.
I like to find the first diffent position of the variable1 compared with variable2.';
$view='<script>
function find_mismatch($str1, $str2) {
$count = 0;
while ($count < (min(strlen($str1), strlen($str2)))) {
if (substr($str1, $count, 1) != substr($str2, $count, 1)) {
// $count is the first different character
return $count;
} // end of if()
$count += 1;
} // end of while()
return 0;
} // end of function
</script>';
echo $view;
echo $count.'<br>';
echo $find_mismatch.'<br>';
echo find_mismatch($str1, $str2);
?>
As I make $count echo, I was expecting that it produce “181”,
But as you see at the page of http://dot.kr/x-test/php/delimeter_position2.php ,
it produces nothing, so I make it $find_mismatch echo,
But it neither work.
Fatal error: Call to undefined function find_mismatch() in C:\myAapm\APM_Setup\htdocs\x-test\php\delimeter_position2.php on line 37
Where did you put the function in your code? If it’s in an include(), is it somewhere accessible? I copied your code, added the function in, and it worked just fine. The second way you call it is correct: