我如何检查一个字母是否已经用PHP改变了?

时间:2022-02-18 07:32:50

How do I check if a letter has been changed in a word using PHP?

我如何检查一个字母是否已经用PHP改变了?

For example, let's say the word someword changed to somEwOrd. How would I be able to check for the change if it's in a string or an array using PHP?

例如,让我们说这个词由someword变成了someword。如果更改位于字符串或数组中,如何使用PHP检查更改?

3 个解决方案

#1


2  

If you need to know which letters have changed... Assuming your word is stored in a variable, you can access it via array indices in a loop to see which characters have changed.

如果你想知道哪些字母变了……假设您的单词存储在一个变量中,您可以通过一个循环中的数组索引访问它,以查看哪些字符已经更改。

$word = "someword";
$changedword = "someWoRd";

// array of changed letters by index
$changedletters = array();
for ($i=0; $i<strlen($word); $i++) {
  if ($changedword[$i] !== $word[$i]) {
    $changedletters[$i] = $word[$i];
  }
}

var_dump($changedletters);
array(2) {
  [4]=>
  string(1) "w"
  [6]=>
  string(1) "r"
}

Or to track changes like "from w to W"

或者跟踪“从w到w”的变化

for ($i=0; $i<strlen($word); $i++) {
  if ($changedword[$i] !== $word[$i]) {
    $changedletters[$i] = array("from"=>$word[$i], "to"=>$changedword[$i]);
  }
}

array(2) {
  [4]=>
  array(2) {
    ["from"]=>
    string(1) "w"
    ["to"]=>
    string(1) "W"
  }
  [6]=>
  array(2) {
    ["from"]=>
    string(1) "r"
    ["to"]=>
    string(1) "R"
  }
}

#2


1  

Just use strtolower(), and check if strtolower($string1) == strtolower($string2).

只需使用strtolower(),并检查strtolower($string1) == strtolower($string2)。

Or strcasecmp, check if strcasecmp($string1,$string2) == 0.

或者strcasecmp,检查strcasecmp($string1,$string2)是否= 0。

#3


0  

You would probably want to compare the strings to each other twice:

你可能会想要将这些字符串进行两次比较:

  1. Compare string with case insensitivity, to determine if they represent the same string regardless of case (using e.g. strcasecmp)
  2. 比较字符串和大小写不敏感的情况,以确定它们是否代表相同的字符串,而不管大小写(例如,使用strcasecmp)
  3. If #1 is true, then compare strings with case sensitivity (using e.g. strcmp).
  4. 如果#1为真,则比较字符串与区分大小写(使用例如strcmp)。

If #1 is true and #2 is false, then you know you are dealing with the same string but difference letter cases.

如果#1为真,#2为假,那么您就知道您处理的是相同的字符串,但是不同的字母情况。

#1


2  

If you need to know which letters have changed... Assuming your word is stored in a variable, you can access it via array indices in a loop to see which characters have changed.

如果你想知道哪些字母变了……假设您的单词存储在一个变量中,您可以通过一个循环中的数组索引访问它,以查看哪些字符已经更改。

$word = "someword";
$changedword = "someWoRd";

// array of changed letters by index
$changedletters = array();
for ($i=0; $i<strlen($word); $i++) {
  if ($changedword[$i] !== $word[$i]) {
    $changedletters[$i] = $word[$i];
  }
}

var_dump($changedletters);
array(2) {
  [4]=>
  string(1) "w"
  [6]=>
  string(1) "r"
}

Or to track changes like "from w to W"

或者跟踪“从w到w”的变化

for ($i=0; $i<strlen($word); $i++) {
  if ($changedword[$i] !== $word[$i]) {
    $changedletters[$i] = array("from"=>$word[$i], "to"=>$changedword[$i]);
  }
}

array(2) {
  [4]=>
  array(2) {
    ["from"]=>
    string(1) "w"
    ["to"]=>
    string(1) "W"
  }
  [6]=>
  array(2) {
    ["from"]=>
    string(1) "r"
    ["to"]=>
    string(1) "R"
  }
}

#2


1  

Just use strtolower(), and check if strtolower($string1) == strtolower($string2).

只需使用strtolower(),并检查strtolower($string1) == strtolower($string2)。

Or strcasecmp, check if strcasecmp($string1,$string2) == 0.

或者strcasecmp,检查strcasecmp($string1,$string2)是否= 0。

#3


0  

You would probably want to compare the strings to each other twice:

你可能会想要将这些字符串进行两次比较:

  1. Compare string with case insensitivity, to determine if they represent the same string regardless of case (using e.g. strcasecmp)
  2. 比较字符串和大小写不敏感的情况,以确定它们是否代表相同的字符串,而不管大小写(例如,使用strcasecmp)
  3. If #1 is true, then compare strings with case sensitivity (using e.g. strcmp).
  4. 如果#1为真,则比较字符串与区分大小写(使用例如strcmp)。

If #1 is true and #2 is false, then you know you are dealing with the same string but difference letter cases.

如果#1为真,#2为假,那么您就知道您处理的是相同的字符串,但是不同的字母情况。