替换字符串中的重复字符串 [英] Replace repeating strings in a string
问题描述
我正在尝试在字符串中查找(并替换)重复的字符串.
I'm trying to find (and replace) repeated string in a string.
我的字符串看起来像这样:
My string can look like this:
Lorem ipsum dolor坐 amet坐amet坐amet坐 nostrud锻炼 amit坐 ullamco labouris nisi ut aliquip ex commodo conequat.
Lorem ipsum dolor sit amet sit amet sit amet sit nostrud exercitation amit sit ullamco laboris nisi ut aliquip ex ea commodo consequat.
这应该变成:
Lorem ipsum dolor坐 amet坐 nostrud锻炼 amit坐 ullamco laburis nisi ut aliquip从commodo的后果.
Lorem ipsum dolor sit amet sit nostrud exercitation amit sit ullamco laboris nisi ut aliquip ex ea commodo consequat.
请注意,由于未重复执行坐位,因此不会将其删除.
Note how the amit sit isn't removed since its not repeated.
或者字符串可以像这样:
Or the string can be like this:
Lorem ipsum dolor坐 amet()坐amet()坐amet()坐 nostrud exercitation ullamco labis nisi ut aliquip aliquip 从commodo的后果.
Lorem ipsum dolor sit amet () sit amet () sit amet () sit nostrud exercitation ullamco laboris nisi ut aliquip aliquip ex ea commodo consequat.
应变为:
Lorem ipsum dolor坐 amet()坐 nostrud exercitation ullamco labouris ni ut aliquip 从中得出结论.
Lorem ipsum dolor sit amet () sit nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
所以它不仅是a-z,而且还可以有其他(ascii)字符.如果有人可以帮助我,我很高兴.
So its not just a-z but can also have other (ascii) chars. I'm verry happy if someone can help me with this.
下一步是匹配(并替换)这样的内容:
The next step would be to match (and replace) something like this:
2个问题3个问题4个问题5个问题
2 questions 3 questions 4 questions 5 questions
将变为:
2个问题
2 questions
最终输出中的数字可以是2、3、4中的任何数字,这无关紧要.在最后一个示例中,只会有不同的数字,但单词将是相同的.
The number in the final output can be any number 2,3,4, it doesn't matter. There will only be different numbers in the final example but the words will be the same.
推荐答案
第一个任务解决方案代码:
First task solution code:
<?php
function split_repeating($string)
{
$words = explode(' ', $string);
$words_count = count($words);
$need_remove = array();
for ($i = 0; $i < $words_count; $i++) {
$need_remove[$i] = false;
}
// Here I iterate through the number of words that will be repeated and check all the possible positions reps
for ($i = round($words_count / 2); $i >= 1; $i--) {
for ($j = 0; $j < ($words_count - $i); $j++) {
$need_remove_item = !$need_remove[$j];
for ($k = $j; $k < ($j + $i); $k++) {
if ($words[$k] != $words[$k + $i]) {
$need_remove_item = false;
break;
}
}
if ($need_remove_item) {
for ($k = $j; $k < ($j + $i); $k++) {
$need_remove[$k] = true;
}
}
}
}
$result_string = '';
for ($i = 0; $i < $words_count; $i++) {
if (!$need_remove[$i]) {
$result_string .= ' ' . $words[$i];
}
}
return trim($result_string);
}
$string = 'Lorem ipsum dolor sit amet sit amet sit amet sit nostrud exercitation amit sit ullamco laboris nisi ut aliquip ex ea commodo consequat.';
echo $string . '<br>';
echo split_repeating($string) . '<br>';
echo 'Lorem ipsum dolor sit amet sit nostrud exercitation amit sit ullamco laboris nisi ut aliquip ex ea commodo consequat.' . '<br>' . '<br>';
$string = 'Lorem ipsum dolor sit amet () sit amet () sit amet () sit nostrud exercitation ullamco laboris nisi ut aliquip aliquip ex ea commodo consequat.';
echo $string . '<br>';
echo split_repeating($string) . '<br>';
echo 'Lorem ipsum dolor sit amet () sit nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.';
?>
第二个任务解决方案代码:
Second task solution code:
<?php
function split_repeating($string)
{
$words = explode(' ', $string);
$words_count = count($words);
$need_remove = array();
for ($i = 0; $i < $words_count; $i++) {
$need_remove[$i] = false;
}
for ($j = 0; $j < ($words_count - 1); $j++) {
$need_remove_item = !$need_remove[$j];
for ($k = $j + 1; $k < ($words_count - 1); $k += 2) {
if ($words[$k] != $words[$k + 2]) {
$need_remove_item = false;
break;
}
}
if ($need_remove_item) {
for ($k = $j + 2; $k < $words_count; $k++) {
$need_remove[$k] = true;
}
}
}
$result_string = '';
for ($i = 0; $i < $words_count; $i++) {
if (!$need_remove[$i]) {
$result_string .= ' ' . $words[$i];
}
}
return trim($result_string);
}
$string = '2 questions 3 questions 4 questions 5 questions';
echo $string . '<br>';
echo split_repeating($string) . '<br>';
echo '2 questions';
?>
这篇关于替换字符串中的重复字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!