【问题标题】:How to remove duplicate words from the string in PHP如何从PHP中的字符串中删除重复的单词
【发布时间】:2018-07-31 22:55:25
【问题描述】:

我有以下带有重复单词的字符串:

$string = 'This is titleThis is titleThis is Title';

现在我想删除重复的单词 This is titleThis is Title 在我的字符串中重复了两次。

我正在寻找这个输出:This is title

我尝试过使用implode(',',array_unique(explode(',', $string))),但运气不佳。

【问题讨论】:

  • preg_replace("/^(.+)\1+$/","$1",$string); 也许?你的要求很模糊。
  • “就是这样”怎么样?您要删除第二个“it”和/或“is”吗?
  • 将字符串分解为空格而不是,。我在您的字符串中没有看到任何 ,
  • 我正在从数据库中获取重复的单词,例如 This is titleThis is titleThis is Title,我想删除重复的单词并希望显示为 This is title

标签: php string duplicates


【解决方案1】:

请使用此代码。在 space 而不是 , 上分解字符串

<?php
$string = 'This is titleThis is titleThis is Title';

echo implode(' ',array_unique(explode(' ', $string)));

【讨论】:

  • 使用你的代码后,我得到了这个输出:This is titleThis Title 但我想要This is title 结果。
  • 我正在尝试,但问题以另一种方式出现
  • 你能帮忙吗?谢谢:)
猜你喜欢
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
  • 2013-08-11
  • 1970-01-01
  • 2014-02-06
  • 2019-11-29
相关资源
最近更新 更多