计算文件中两对字符串之间的最大距离(位置)
calculate the maximum distance (position) between two pair strings in a file
我想计算文件中两个字符串之间的最大距离。
Str1 和 str2 可以相同,例如 max_dist('.', '.', $text)
假设:public static function max_dist($str1, $str2, $text)
例如维基百科中的文本:
Though not explicitly anarchist, they organized by rank and file
democracy, embodying a spirit of resistance that has inspired many
Anglophone syndicalists.
我想找出“,”和“.”之间的最大距离在整个文件中。请注意 ',' 和 '.'在所有文本中重复出现,是否成对出现。
示例应该return 125 作为第一个逗号和点之间的距离。
我正在开发以下代码,但只有在没有要查找的重复字符串的情况下才有效,到目前为止:
public static function max_dist($str1, $str2, $f)
{
$len = strlen($f);
$max_dist = 0;
$pos_a = 0; $pos_b = 1;
while (true) {
$a = strpos($f, $str1, $pos_a);
$b = strpos($f, $str2, $pos_b);
if (!($a && $b)) break;
if ($a == $b)
$b = strpos($f, $str2, $a + 1);
if (!$a || !$b) continue;
//if()
$abs = abs($a - $b);
if ($abs > $max_dist) $max_dist = $abs;
$pos_a = $a + 1;
$pos_b = $b + 1;
}
return $max_dist;
}
有什么想法吗?我找到了函数 strstr.php 但它没有偏移选项
编辑澄清:
假设文本如下:
The House of Plantagenet (1154–1485) was the royal house of all the
English kings from Henry II to Richard III, including the Angevin
kings and the houses of Lancaster and York. In addition to the
traditional judicial, feudal and military roles of the king, the
Plantagenets had duties to the realm that were underpinned by a
sophisticated justice system.
您的函数 (@bogdan),return是所有函数中的最大值,即:246。
我想计算所有的最大值,但是成对计算,在这种情况下应该是:139 (judicial,
to system.
)
我觉得你的功能太复杂了。只需将 strpos
与 strrpos
:
一起使用
$string = "Though not explicitly anarchist, they organized by rank and file democracy, embodying a spirit of resistance that has inspired many Anglophone syndicalists.";
function max_dist($needle1, $needle2, $string)
{
$first = strpos($string, $needle1);
$last = strrpos($string, $needle2);
return $last - $first;
}
echo max_dist(',', '.', $string);
如果 .
可以在 ,
之前,您可能需要第二次交换参数 ant 检查。或者使用 abs
函数。
更新
如果我没理解错的话。那么你应该保留未处理的字符串:
$string = "The House of Plantagenet (1154–1485) was the royal house of all the English kings from Henry II to Richard III, including the Angevin kings and the houses of Lancaster and York. In addition to the traditional judicial, feudal and military roles of the king, the Plantagenets had duties to the realm that were underpinned by a sophisticated justice system.";
function max_dist($needle1, $needle2, $string)
{
$distances = [];
$string_left = $string;
while (strpos($string_left, $needle1) !== false && strpos($string_left, $needle2) !== false) {
$first = strpos($string_left, $needle1);
$last = strpos($string_left, $needle2);
$distance = abs($last - $first);
$distances[] = $distance;
$offset = max([$first, $last]);
$string_left = substr($string_left, $offset);
}
return $distances;
}
echo max(max_dist(',', '.', $string));
我想计算文件中两个字符串之间的最大距离。
Str1 和 str2 可以相同,例如 max_dist('.', '.', $text)
假设:public static function max_dist($str1, $str2, $text)
例如维基百科中的文本:
Though not explicitly anarchist, they organized by rank and file democracy, embodying a spirit of resistance that has inspired many Anglophone syndicalists.
我想找出“,”和“.”之间的最大距离在整个文件中。请注意 ',' 和 '.'在所有文本中重复出现,是否成对出现。
示例应该return 125 作为第一个逗号和点之间的距离。
我正在开发以下代码,但只有在没有要查找的重复字符串的情况下才有效,到目前为止:
public static function max_dist($str1, $str2, $f)
{
$len = strlen($f);
$max_dist = 0;
$pos_a = 0; $pos_b = 1;
while (true) {
$a = strpos($f, $str1, $pos_a);
$b = strpos($f, $str2, $pos_b);
if (!($a && $b)) break;
if ($a == $b)
$b = strpos($f, $str2, $a + 1);
if (!$a || !$b) continue;
//if()
$abs = abs($a - $b);
if ($abs > $max_dist) $max_dist = $abs;
$pos_a = $a + 1;
$pos_b = $b + 1;
}
return $max_dist;
}
有什么想法吗?我找到了函数 strstr.php 但它没有偏移选项
编辑澄清:
假设文本如下:
The House of Plantagenet (1154–1485) was the royal house of all the English kings from Henry II to Richard III, including the Angevin kings and the houses of Lancaster and York. In addition to the traditional judicial, feudal and military roles of the king, the Plantagenets had duties to the realm that were underpinned by a sophisticated justice system.
您的函数 (@bogdan),return是所有函数中的最大值,即:246。
我想计算所有的最大值,但是成对计算,在这种情况下应该是:139 (judicial,
to system.
)
我觉得你的功能太复杂了。只需将 strpos
与 strrpos
:
$string = "Though not explicitly anarchist, they organized by rank and file democracy, embodying a spirit of resistance that has inspired many Anglophone syndicalists.";
function max_dist($needle1, $needle2, $string)
{
$first = strpos($string, $needle1);
$last = strrpos($string, $needle2);
return $last - $first;
}
echo max_dist(',', '.', $string);
如果 .
可以在 ,
之前,您可能需要第二次交换参数 ant 检查。或者使用 abs
函数。
更新
如果我没理解错的话。那么你应该保留未处理的字符串:
$string = "The House of Plantagenet (1154–1485) was the royal house of all the English kings from Henry II to Richard III, including the Angevin kings and the houses of Lancaster and York. In addition to the traditional judicial, feudal and military roles of the king, the Plantagenets had duties to the realm that were underpinned by a sophisticated justice system.";
function max_dist($needle1, $needle2, $string)
{
$distances = [];
$string_left = $string;
while (strpos($string_left, $needle1) !== false && strpos($string_left, $needle2) !== false) {
$first = strpos($string_left, $needle1);
$last = strpos($string_left, $needle2);
$distance = abs($last - $first);
$distances[] = $distance;
$offset = max([$first, $last]);
$string_left = substr($string_left, $offset);
}
return $distances;
}
echo max(max_dist(',', '.', $string));