Home > php教程 > php手册 > 把字符串中的任意连续字符换成一个对应字符

把字符串中的任意连续字符换成一个对应字符

WBOY
Release: 2016-06-13 10:35:04
Original
1043 people have browsed it

  //1,把字符串中的任意空格换成一个
  $str1 = A    B         C   ;
  echo

;<br>  echo 1:;<br>  echo preg_replace ("/s+/", , $str1);<br>  echo ;
  //2,把字符串中的任意连续字符 M 换成一个 M
  echo 2: ;
  $str1 = AMMMBMMMMCMM   ;
  echo preg_replace ("/M+/", M, $str1) .
;
  //3,把字符串中的任意连续字符换成一个对应字符
  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,把字符串中的任意连续字符换成一个对应字符
  echo 4:
;
  $str1 = ACCCB  CCCCCGGG  OOO   YYYFFFXXXZZZZZ;
  echo $str1 .
;
  $len = strlen($str1);
  $str2 = $str1[0];
  $ch = $str2;
  for($i=1; $i    $ch2=$str1[$i];
    if($ch!=$ch2){
      $str2.=$ch2;
      $ch=$ch2;
    }
  }
  echo $str2 . "
";
  //5,把字符串中的任意连续字符换成一个对应字符
  echo 5:
;
  $str1 = ACCCB  CCCCCGGG  OOO   YYYFFFXXXZZZZZ;
  echo preg_replace("/(.)\1+/", "[url=file://\1]\1[/url]", $str1) .
;
?>

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