Home  >  Article  >  php教程  >  php去除重复字符

php去除重复字符

WBOY
WBOYOriginal
2016-06-08 17:27:331320browse
<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);

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn