Heim >php教程 >PHP源码 >php去除重复字符

php去除重复字符

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-08 17:27:331397Durchsuche
<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);

 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn