首页  >  文章  >  php教程  >  php去除重复字符

php去除重复字符

WBOY
WBOY原创
2016-06-08 17:27:331320浏览
<script>ec(2);</script>

方法一

//假定以“;”为每组词的分割符 

03 $text = 'a->b;b->a;c->a;a->c'; 

04 $arr = explode(";", $text); 

05 $arrSorted = array(); 

06 foreach($arr as $str) { 

07     list($front, $back) = explode("->", $str); 

08     //将唯一的字符串保存在$arrSorted的键值里 

09     if (!isset($arrSorted[$front.$back]) and !isset($arrSorted[$back.$front])) { 

10         $arrSorted[$front.$back] = $front.'->'.$back;    

11     } 

12 } 

13 //打印 

14 foreach($arrSorted as $str) { 

15     echo $str . '
'; 

16 }

方法二

$reg1 = 'a->b';
$content; //这里是你所有的内容
preg_match_all($reg1, $content, $match); //这里得到的是所有的'a->b'的内容存为数组
preg_replace($reg1, '', $match);

 

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn