php基础知识的灵活使用

来源:互联网 发布:时尚杂志推荐 知乎 编辑:程序博客网 时间:2024/06/06 23:28

问题描述:在后台比较将两个名称md5后的字符串(str1和str2),如果str1和str2在相同位置的字符相同则记10分,如果相连的字符有n个,则在原来基础上再加上 n*10,最后得出的总分数为他们之间的缘分。

举例:

Str1 = abced;

Str2 = abf1d;

则得分为(10 + 10 + 2*10 + 10) = 50分


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
</head>
<body>
<center>
    <form action="index.php?r=yuanf/index" method="post">
    <table>
        <tr>
            <td><input type="text" name="uname1"/></td>
            <td>和</td>
            <td><input type="text" name="uname2"/></td>
            <td><input type="submit" value="计算得分"/></td>
        </tr>
        <tr>
            <td colspan="4"><textarea rows="5" cols="60"></textarea></td>
        </tr>
    </table>
    </form>
</center>
</body>
</html>

<?php
namespace frontend\controllers;


use Yii;
use common\models\LoginForm;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
/**
 * Yuan controller
 */


class YuanfController extends Controller
{
    public $enableCsrfValidation = false;
    public function  actionYuanf(){
        return $this->render('yuanf.html');
    }


    //测试缘分
    public  function  actionIndex(){
        $uname1=md5($_POST['uname1']);
        $uname2=md5($_POST['uname2']);
        //echo $uname1;die;
        $length=strlen($uname1);//长度


        $score=0;//重复数
        $repeat=1;//重复长度


        for ($i=0; $i < $length; $i++) {
            if($uname1[$i]==$uname2[$i]){
                $score+=10;


                if($i>0){
                    if($uname1[$i-1]==$uname2[$i-1]){
                        $repeat++;//计算重复值得长度
                    }else{
                        if($repeat>1){
                            $score+=$repeat*10;//不重复后直接计算结果
                            $repeat=1;
                        }
                    }
                }
            }
        }
//最后得到结果
        if($repeat>1){
            $score+=$repeat*10;
        }
//输出
        echo "<script>alert('你们的缘分指数是:".$score."分');location.href='index.php?r=yuanf/yuanf'</script>";
    }

0 0