从PHP中的字符串中删除重复 [英] remove duplicate from string in PHP

查看:117
本文介绍了从PHP中的字符串中删除重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



所以我的字符串看起来像这样;

  $ str ='一,二,一,五,七,袋,茶' 

我可以做到这一点,将字符串扩大到值,然后进行比较,但我认为这将会很慢。那么preg_replace()会更快吗?任何一个使用这个功能?

解决方案

最短的代码是:

  $ str = implode(',',array_unique(explode(',',$ str))); 

如果它是最快的...我不知道,明显地循环更快。



参考: implode array_unique 爆炸


I am looking for the fastest way to remove duplicate values in a string separated by commas.

So my string looks like this;

$str = 'one,two,one,five,seven,bag,tea';

I can do it be exploding the string to values and then compare, but I think it will be slow. what about preg_replace() will it be faster? Any one did it using this function?

解决方案

The shortest code would be:

$str = implode(',',array_unique(explode(',', $str)));

If it is the fastest... I don't know, it is probably faster then looping explicitly.

Reference: implode, array_unique, explode

这篇关于从PHP中的字符串中删除重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆