2 个子串之间的距离

发布于 2024-11-07 15:31:53 字数 83 浏览 2 评论 0原文

如何找到给定字符串中两个子字符串值之间的距离?例如,如果我有“terrific”这个词,并且我想找到“i”之间的距离(相距 1 个空格)。感谢您的帮助。

How can I find the distance between two substrings values in a given string? For example, if I have the word terrific, and I wanted to find the distance between the "i"s (1 space apart). Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜唯美灬不弃 2024-11-14 15:31:53

以下是一些带有内嵌注释的方法:

// We take our string
$mystring = "terrific";

// Then the first character we want to look for
$mychar1 = "i";
$mychar2 = "i";

// Now we get the position of the first character
$position1 = strpos( $mystring, $mychar1);

// Now we use the last optional parameter offset to get the next i
// We have to go one beyond the previous position for this to work
// Properly
$position2 = strpos( $mystring, $mychar2, ($position1 + 1) );

// Then we get the distance
echo "Distance is: " . ($position2 - $position1) . "\n";

// We can also use strrpos to find the distance between the first and last i
// if there are more than one
$mystring2 = "terrific sunshine";
$position2 = strrpos( $mystring2, $mychar2);

echo "Distance is: " . ($position2 - $position1) . "\n";

Here are a few methods with comments inline:

// We take our string
$mystring = "terrific";

// Then the first character we want to look for
$mychar1 = "i";
$mychar2 = "i";

// Now we get the position of the first character
$position1 = strpos( $mystring, $mychar1);

// Now we use the last optional parameter offset to get the next i
// We have to go one beyond the previous position for this to work
// Properly
$position2 = strpos( $mystring, $mychar2, ($position1 + 1) );

// Then we get the distance
echo "Distance is: " . ($position2 - $position1) . "\n";

// We can also use strrpos to find the distance between the first and last i
// if there are more than one
$mystring2 = "terrific sunshine";
$position2 = strrpos( $mystring2, $mychar2);

echo "Distance is: " . ($position2 - $position1) . "\n";
清浅ˋ旧时光 2024-11-14 15:31:53
$haystack = 'terrific';
$needle = 'i';

$distance = false;
$pos1 = strpos($haystack,$needle);
if ($pos1 !== false) {
   $pos2 = strpos($haystack,$needle,$pos1+1);
   if ($pos2 !== false) {
      $distance = $pos2 - $pos1;
   }
}

编辑

$haystack = 'terrific';
$needle = 'i';

$distance = false;
$needlePositions = array_keys(array_intersect(str_split($haystack),array($needle)));
if (count($needlePositions) > 1) {
   $distance = $needlePositions[1] - $needlePositions[0];
}
$haystack = 'terrific';
$needle = 'i';

$distance = false;
$pos1 = strpos($haystack,$needle);
if ($pos1 !== false) {
   $pos2 = strpos($haystack,$needle,$pos1+1);
   if ($pos2 !== false) {
      $distance = $pos2 - $pos1;
   }
}

EDIT

or

$haystack = 'terrific';
$needle = 'i';

$distance = false;
$needlePositions = array_keys(array_intersect(str_split($haystack),array($needle)));
if (count($needlePositions) > 1) {
   $distance = $needlePositions[1] - $needlePositions[0];
}
子栖 2024-11-14 15:31:53

您可以处理的选项有很多,但这就是开始的地方。

http://php.net/manual/en/function.strrpos.php

There are so many options that you could handle, but this is where to start.

http://php.net/manual/en/function.strrpos.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文