php - 如何将非常相似的字符串中的方差输出到数组中?

标签 php

我有类似这个字符串数组的东西,可以看出它们非常相似,除了字符串中相同的一个位置:

  $strings = [
        'This is +1% better than last time!',
        'This is +2% better than last time!',
        'This is +3% better than last time!',
        'This is +4% better than last time!',
        'This is +5% better than last time!',
        ...
    ];

  // Psuedo code

由此我想结束

$array = [
    '1',
    '2',
    '3',
    '4',
    '5',
    ...
];
// And preferrably
$string = 'This is +%s% better than last time!';

通过一个函数,该函数接受任何相似字符串的数组并输出它们中实际不同的内容。

最佳答案

如果您知道相似字符串数组仅在一个地方不同,那么您可以从头开始比较它们直到它们不同,然后从末尾比较它们直到它们不同并记录这些偏移量。然后从数组中的每个字符串中提取从第一个差异开始直到最后一个差异的字符串。

关于php - 如何将非常相似的字符串中的方差输出到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38807195/

相关文章:

php - 需要一个循环发送功能的想法

php - 在 Android 聊天应用程序中实现规则的最佳方式

php - 如何在 php 中调用 curl?

php - 如何从类中实例化 $this 类的对象? PHP

javascript - 如何从 URL : http://www. 获取 "foo"loremipsum.com/mysite.html#foo

php - 在 curl 中,url 请求的加载时间是多少

php - Codeigniter SQL 查询构建

php - Zend Framework 插件自动加载

php - Notepad++ - 是否可以使用自定义字体?

PHP 中的 JAVA DES