Replace any continuous character in the string with a corresponding character _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Replace any consecutive character in the string with a corresponding character .? Php1, replace any space in the string with a $ str1ABC; echoxmp; echo1:; echopreg_replace (s +, $ str1); echoxmp; 2, replace any consecutive character M in the string // 1. replace any space in the string with one
$ Str1 = a B C;
Echo ; <BR> echo 1:; <BR> echo preg_replace ("/s +/", $ str1); <BR> echo ;
// 2. replace any consecutive character M in the string with one M
Echo 2 :;
$ Str1 = AMMMBMMMMCMM;
Echo preg_replace ("/M +/", M, $ str1 ).
;
// 3. replace any consecutive character in the string with a corresponding character
Echo 3:
;
$ Str1 = acccb cccccggg ooo yyyfffxxxzzzzz;
Echo $ str1.
;
$ Str2 =;
$ I = 0;
For ($ I = 0; $ I {
$ Str2 = $ str2. $ str1 {$ I };
$ J = 1;
While ($ str1 {$ I }=$ str1 {$ I + $ j })
$ J ++;
$ I = $ I + $ j-1;
}
Echo $ str2.
;
// 4. replace any consecutive character in the string with a corresponding character
Echo 4:
;
$ Str1 = acccb cccccggg ooo yyyfffxxxzzzzz;
Echo $ str1.
;
$ Len = strlen ($ str1 );
$ Str2 = $ str1 [0];
$ Ch = $ str2;
For ($ I = 1; $ I <$ len; $ I ++ ){
$ Ch2 = $ str1 [$ I];
If ($ ch! = $ Ch2 ){
$ Str2. = $ ch2;
$ Ch = $ ch2;
}
}
Echo $ str2 ."
";
// 5. replace any consecutive character in the string with a corresponding character
Echo 5:
;
$ Str1 = acccb cccccggg ooo yyyfffxxxzzzzz;
Echo preg_replace ("/(.) \ 1 +/", "[url = file: // \ 1] \ 1 [/url]", $ str1 ).
;
?>

Http://www.bkjia.com/PHPjc/508229.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/508229.htmlTechArticle? Php // 1, replace any space in the string with A $ str1 = a B C; echo xmp; echo 1:; echo preg_replace ("/s + /",, $ str1); echo/xmp; // 2, replace any consecutive character M in the string...

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.