Replace any consecutive character in the string with a corresponding character.
Source: Internet
Author: User
Replace any consecutive character in the string with a corresponding character. Read and replace any consecutive character in the string with a corresponding character ,? Php // 1, replace any space in the string with a $ str1 = "> <LINKhref =" http://www.php100.com//statics/style/headfloor_950_081205.css "type = te // 1. replace any space in the string with one
$ Str1 = 'a B C ';
Echo' '; <BR> & nbsp; echo '1:'; <BR> & nbsp; echo preg_replace ("/\ s +/", '', $ str1 ); <BR> & nbsp; 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 ).'
';
?>
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.