字符串处理

题目要求

代码

<?php
function getBack($file){
    $names=explode('.',$file);
    $back=array();
    for ($i=count($names)-1;$i>0;$i--) {
        $back[$i - 1] = $names[$i];
    }
    for ($i=0;$i<count($back);$i++){
        echo '.',$back[$i],' ';
    }
}
$file='test1.txt.exe';
getBack($file);
echo '<br>';

function trans_num($num){
    $nums=str_split($num);
    for ($pos=count($nums)-3;$pos>0;$pos-=3){
        array_splice($nums,$pos,0,',');
    }
    foreach ($nums as $n){
        echo $n;
    }
}
trans_num(1243);
echo '<br>';

function word_large($words){
    $word=explode('_',$words);
    $all='';
    foreach ($word as $w){
        $all.=ucwords($w);
    }
    return $all;
}
echo word_large('hello_world_by');
echo '<br>';

结果:

正则表达式

题目要求

代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
    .name{
        width: 100px;
        height: 50px;
        display: inline-block;
        vertical-align: middle;
    }
    .input{
        width: 400px;
        height: 50px;
        display: inline-block;
    }
</style>
<body>
<?php
$name_err=$password_err=$email_err=$phone_err=$talknum_err=$color_err=$nums_err='';
if (isset($_POST["username"])){
    $username=$_POST["username"];
}else{
    $username='';
    $name_err='请输入用户名';
}
if (!empty($username)){
    if (preg_match("/^[a-zA-Z]\w{6,18}$/i",$username)){
        $name_err='用户名通过!';
    }else{
        $name_err='用户名不规范! 6~18个字符,可使用字母,数字,下划线,需以字母开头';
    }
}else{
    $name_err='请输入用户名!';
}
if (isset($_POST["password"])){
    $password=$_POST["password"];
}else{
    $password='';
    $password_err='请输入密码';
}
if (!empty($password)){
    if (preg_match("/^\S{6,16}$/",$password)){
        $password_err='密码通过!';
    }else{
        $password_err='密码不规范!6~16个字符,区分大小写';
    }
}else{
    $password_err='请输入密码!';
}
if (isset($_POST["email"])){
    $email=$_POST["email"];
}else{
    $email='';
    $email_err='请输入邮箱';
}
if (!empty($email)){
    if (preg_match("/^[a-z0-9]+([._-][a-z0-9]+)*@([0-9a-z]+\.[a-z]{2,14}(\.[a-z]{2})?)$/i",$email)){
        $email_err='邮箱通过!';
    }else{
        $email_err='邮箱不规范!';
    }
}else{
    $email_err='请输入邮箱!';
}
if (isset($_POST["phonenum"])){
    $phonenum=$_POST["phonenum"];
}else{
    $phonenum='';
    $phone_err='请输入手机号';
}
if (!empty($phonenum)){
    if (preg_match("/^((\+86)|(86))?1[3589]\d{9}$/",$phonenum)){
        $phone_err='手机号通过!';
    }else{
        $phone_err='请输入长度为11位的数字!';
    }
}else{
    $phone_err='请输入手机号!';
}
if (isset($_POST["talknum"])){
    $talknum=$_POST["talknum"];
}else{
    $talknum='';
    $talknum_err='请输入座机号';
}
if (!empty($talknum)){
    if (preg_match("/^(0[1-9]{2,3}-)?[0-9]{7,8}$/",$talknum)){
        $talknum_err='座机号通过!';
    }else{
        $talknum_err='请输入正确的数字!';
    }
}else{
    $talknum_err='请输入座机号!';
}
if (isset($_POST["color"])){
    $color=$_POST["color"];
}else{
    $color='';
    $color_err='请输入16进制颜色值';
}
if (!empty($color)){
    if (preg_match("/^#(?:(?:[a-f\d]{3}){1,2})$/i",$color)){
        $color_err='16进制颜色正确!';
    }else{
        $color_err='请输入正确的16进制颜色值!';
    }
}else{
    $color_err='请输入16进制颜色值!';
}
if (isset($_POST["nums"])){
    $nums=$_POST["nums"];
}else{
    $nums='';
    $nums_err='请输入16位数字(每隔四位空格分隔)';
}
if (!empty($nums)){
    if (preg_match("/^\d{4}\s\d{4}\s\d{4}\s\d{4}$/",$nums)){
        $nums_err='数字输入正确!';
    }else{
        $nums_err='请输入正确的数字(每隔四位空格分隔)!';
    }
}else{
    $nums_err='请输入16位数字(每隔四位空格分隔)!';
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
    <div class="name">用户名:</div>
    <div class="input"><input type="text" name="username"><br><?php echo $name_err;?></div>
    <br>
    <div class="name">密码:</div>
    <div class="input"><input name="password"><br><?php echo $password_err;?></div>
    <br>
    <div class="name">电子邮箱:</div>
    <div class="input"><input name="email"><br><?php echo $email_err;?></div>
    <br>
    <div class="name">手机号码:</div>
    <div class="input"><input name="phonenum"><br><?php echo $phone_err;?></div>
    <br>
    <div class="name">座机号码:</div>
    <div class="input"><input name="talknum"><br><?php echo $talknum_err;?></div>
    <br>
    <div class="name">16进制颜色值:</div>
    <div class="input"><input name="color"><br><?php echo $color_err;?></div>
    <br>
    <div class="name">16位数字:</div>
    <div class="input"><input name="nums"><br><?php echo $nums_err;?></div>
    <input type="submit" value="提交">
</form>
</body>
</html>
Last modification:July 12th, 2020 at 01:09 am