Home > Backend Development > PHP Tutorial > php lookandsay

php lookandsay

WBOY
Release: 2016-08-10 08:48:42
Original
1312 people have browsed it
<?php
function lookandsay($str){
        $r = '';
        $m = $str[0];
        $n = 1;

        for($i=1; $i<strlen($str); $i++){
                if($str[$i] == $m){//如果这个字符与上个字符相同则数目加1
                        $n++;
                }else{
                        $r .= $n.$m;//反之,把数目和这个字符追加到返回值
                        $m = $str[$i];//赋值下一个字符
                        $n  = 1;
                }
        }
        return $r.$n.$m;
}

for($i = 0,$str = 1; $i < 9; $i++){
        $str = lookandsay($str);
        print $s."<br/>";
}
?>

Copy after login

The above introduces PHP lookandsay, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template