内置函数在 php 中组合重叠的字符串序列?

发布于 2024-09-03 19:25:46 字数 201 浏览 5 评论 0原文

PHP 中是否有内置函数可以将 2 个字符串合并为 1 个?

示例

$string1 = 'abcde';
$string2 = 'cdefg';

合并得到:abcdefg

如果确切的重叠序列和位置已知,则可以编写代码来合并它们。

Is there a built in function in PHP that would combine 2 strings into 1?

Example:

$string1 = 'abcde';
$string2 = 'cdefg';

Combine to get: abcdefg.

If the exact overlapping sequence and the position are known, then it is possible to write a code to merge them.

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

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

发布评论

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

评论(3

叹倦 2024-09-10 19:25:46

我发现 substr_replace 方法返回有趣的结果。
特别是在使用 url 字符串时。我刚刚写了这个函数。
它似乎非常适合我的需求。
默认情况下,该函数将返回最长的可能匹配。

function findOverlap($str1, $str2){
  $return = array();
  $sl1 = strlen($str1);
  $sl2 = strlen($str2);
  $max = $sl1>$sl2?$sl2:$sl1;
  $i=1;
  while($i<=$max){
    $s1 = substr($str1, -$i);
    $s2 = substr($str2, 0, $i);
    if($s1 == $s2){
      $return[] = $s1;
    }
    $i++;
  }
  if(!empty($return)){
    return $return;
  }
  return false;
}

function replaceOverlap($str1, $str2, $length = "long"){
  if($overlap = findOverlap($str1, $str2)){
    switch($length){
      case "short":
        $overlap = $overlap[0];
        break;
      case "long":
      default:
        $overlap = $overlap[count($overlap)-1];
        break;
    }     
    $str1 = substr($str1, 0, -strlen($overlap));
    $str2 = substr($str2, strlen($overlap));
    return $str1.$overlap.$str2;
  }
  return false;
}

获取最大长度匹配的用法:

echo replaceOverlap("abxcdex", "xcdexfg"); //Result: abxcdexfg

要获取第一个匹配而不是最后一个匹配,请调用如下函数:

echo replaceOverlap("abxcdex", "xcdexfg", “short”); //Result: abxcdexcdexfg

要获取重叠字符串,只需调用:

echo findOverlap("abxcdex", "xcdexfg"); //Result: array( 0 => "x", 1 => "xcdex" )

I found the substr_replace method to return funny results.
Especially when working with url strings. I just wrote this function.
It seems to be working perfectly for my needs.
The function will return the longest possible match by default.

function findOverlap($str1, $str2){
  $return = array();
  $sl1 = strlen($str1);
  $sl2 = strlen($str2);
  $max = $sl1>$sl2?$sl2:$sl1;
  $i=1;
  while($i<=$max){
    $s1 = substr($str1, -$i);
    $s2 = substr($str2, 0, $i);
    if($s1 == $s2){
      $return[] = $s1;
    }
    $i++;
  }
  if(!empty($return)){
    return $return;
  }
  return false;
}

function replaceOverlap($str1, $str2, $length = "long"){
  if($overlap = findOverlap($str1, $str2)){
    switch($length){
      case "short":
        $overlap = $overlap[0];
        break;
      case "long":
      default:
        $overlap = $overlap[count($overlap)-1];
        break;
    }     
    $str1 = substr($str1, 0, -strlen($overlap));
    $str2 = substr($str2, strlen($overlap));
    return $str1.$overlap.$str2;
  }
  return false;
}

Usage to get the maximum length match:

echo replaceOverlap("abxcdex", "xcdexfg"); //Result: abxcdexfg

To get the first match instead of the last match call the function like this:

echo replaceOverlap("abxcdex", "xcdexfg", “short”); //Result: abxcdexcdexfg

To get the overlapping string just call:

echo findOverlap("abxcdex", "xcdexfg"); //Result: array( 0 => "x", 1 => "xcdex" )
初雪 2024-09-10 19:25:46

可以使用substr_replace()strcspn()

$string1 = 'abcde';
$string2 = 'cdefgh';

echo substr_replace($string1, $string2, strcspn($string1, $string2)); // abcdefgh

It's possible using substr_replace() and strcspn():

$string1 = 'abcde';
$string2 = 'cdefgh';

echo substr_replace($string1, $string2, strcspn($string1, $string2)); // abcdefgh
你丑哭了我 2024-09-10 19:25:46

不,没有内置函数,但您可以通过使用 substr 和一个循环来轻松地自己编写一个函数来查看有多少字符串重叠。

No, there is no builtin function, but you can easily write one yourself by using substr and a loop to see how much of the strings that overlap.

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