Home > php教程 > PHP源码 > body text

php去除重复字符

WBOY
Release: 2016-06-08 17:27:33
Original
1325 people have browsed it
<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);

 

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!