Php remove repeated characters-PHP source code

Source: Internet
Author: User
Ec (2); Method 1 assumes that & ldquo; & rdquo; is the separator for each group of words & nbsp; 03 $ texta-& gt; B-& gt; a; c-& gt; a-& gt; c; & nbsp; 04 $ arrexplode (& quot;, $ text); & nbsp; 05 $ arrSortedarray (); & nbsp; 06 foreach ($ arras $ script ec (2); script

Method 1

// Assume that ";" is the delimiter of each group of words.

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 // Save the unique string in the key value of $ arrSorted

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

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

11}

12}

13 // print

14 foreach ($ arrSorted as $ str ){

15 echo $ str .'
';

16}

Method 2

$ Reg1 = 'a-> B ';
$ Content; // all your content here
Preg_match_all ($ reg1, $ content, $ match); // All 'a-> B 'contents are saved as arrays.
Preg_replace ($ reg1, '', $ match );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.