首页 / 教程资源

PHP

发布时间:2023-04-18 03:17:53
  1. 变量的使用

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Document</title>    <style>        .box{            <?php                $a = 200;                echo "width: ".$a."px;";                echo "height: ".$a."px;";                echo "background-color:green;";            ?>        }</style></head><body>   <div class="box">   <!-- <?php    echo '今天是'.date("20y年m月d日 h:i:s");   ?> -->   </div>
<span> <?php $name = 'caochuankuan'; $$name = 'nan';//$caochuankuan = 'nan'; echo $name; echo " "; echo $caochuankuan; echo "<br/>"; $num1 = "1"; $num2 = "66"; echo var_dump($num1); echo "<br/>"; echo var_dump($num2); echo "<br/>"; echo (int)$num1+(int)$num2; ?> </span>
</body></html>

2.函数 变量作用域

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>21103</title></head><body>
<?php
$a = "666"; $b = "777"; function f1() { // $a = "111"; global $a; echo "<div style='color:blue;font-size:25px;'>".$a."</div>"; echo "<div style='color:pink;'>".$GLOBALS["b"]."</div>"; echo var_dump($GLOBALS); echo "<br>"; } $ff1 = "f1"; $ff1($a);
$c = function(){ echo "<br>"; echo "123"; }; $c();
echo("<br>");
// $a = "666"; // function f1($string) { // // $a = "111"; // echo "<div>".$string."</div>"; // } // f1($a);
//关联数组 $array2 = array( "name" => "yifeng", "age" => 3, ); echo var_dump($array2); echo("<br>"); echo $array2["name"]; echo("<br>"); echo $array2["age"]; ?>
<script> var aa = "888";//全局变量 function f2(){ var aa = "999";//局部变量 console.log(aa); } f2(); console.log(aa); var bb = function(){ console.log("aaaaaaaaaaa"); }; bb();</script>
</body></html>

3.get

index.html

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Document</title></head><body>    <form action="aaa.php" method="get">        姓名:<input type="text" name="fn">        <br>        班级:<select name="fb" id="">            <option value="1">1班</option>            <option value="2">2班</option>        </select>        <br>        <input type="submit">    </form></body></html>

aaa.php

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>21103</title></head><body>    <?php
$ikun = array( "name" => "蔡徐坤", "sex" => "男", "like1" => "唱", );
$ikun["like2"] = "跳"; $ikun["like3"] = "rap";
foreach($ikun as $key => $i) { echo "<div href='#' style='color:orange;'>$key :$i</div>"; echo "<br>"; }
echo $_SERVER["SERVER_PORT"]; echo "<br>"; echo $_SERVER["SCRIPT_FILENAME"];
echo "<br>"; echo "姓名:".$_GET['fn']; echo "<br>"; echo "班级:".$_GET['fb']."班";
?></body></html>

4.cookie post

index.php

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>21103</title>    <script src="jquery-3.6.0.js"></script>    <script src="jquery.cookie.js"></script>    <script>        $(function(){            $.cookie("ck1","yifeng");            $("#submit").click(function(){                var zh1 = $("#submit").parent().children().eq(0).val();                console.log(zh1);                var zh2 = $("#submit").parent().children().eq(2).val();                console.log(zh2);                $("#submit").parent().children().eq(0).val($.cookie("zh1",zh1));                $("#submit").parent().children().eq(2).val($.cookie("zh2",zh2));            })        })</script></head><body>    <form enctype="multipart/form-data" action="aaa.php" method="POST">        账号:<input type="text" name="text1">        <br>        密码:<input type="text" name="text2">        <br>        <input type="file" name="file1">        <br>        <input type="submit" id="submit">    </form>
<?php
var_dump($_COOKIE);
?>
</body></html>

aaa.php

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>21103</title></head><body>    <?php
echo $_POST["text1"]; echo "<br>"; echo ($_FILES['file1']['name']); echo "<br>"; echo ($_FILES['file1']['tmp_name']); move_uploaded_file($_FILES['file1']['tmp_name'],"./uploads/".$_FILES['file1']['name']); ?></body></html>

5.session

index.php

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>21103</title></head><body>    <form action="123.php" method="GET">        姓名:<input type="text" name="tx1" <?php        session_id("1234");        session_start();        echo "value=".$_SESSION["姓名"];        ?>>        <br>        学号:<input type="number" name="nu1" value=<?php        echo '"'.$_SESSION["学号"].'"';        ?>>        <br>        <input type="submit">    </form></body></html>

123.php

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>21103</title></head><body>        <?php
// print_r($_REQUEST); // echo "<br>"; // // print_r($_SERVER); // echo $_SERVER["REQUEST_METHOD"]; // echo "<br>"; $req; if ($_SERVER["REQUEST_METHOD"] == "GET"){ $req = $_GET; } else if ($_SERVER["REQUEST_METHOD"] == "POST") { $req = $_POST; } echo "<br>"; print_r($req);
session_id("1234"); session_start(); $_SESSION["姓名"] =$req["tx1"]; $_SESSION["学号"] =$req["nu1"]; print_r($_SESSION); echo session_id();
?>
</body></html>

6.自动登录

index.php

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>21103</title>    <script src="jquery-3.6.0.js"></script>    <script src="jquery.cookie.js"></script>    <script>        // 3.判断cookie数据是否和服务器session id一致,是就跳转到登陆后234.php        $(function(){
// $(".aa").click(function(){ // // location.href = "123.php"; // $(location).attr("href","123.php"); // })
<?php session_id($_COOKIE["sessionID"]); session_start(); if($_SESSION["tx1"]){ echo '$(location).attr("href","234.php");'; } ?> })</script></head><body> <!-- <div class="aa">aaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div> --> <form action="123.php" method="POST"> 账号:<input type="text" name="tx1"> <br> 密码:<input type="password" name="pw1"> <br> <input type="submit"> </form>
<?php // echo $_COOKIE["sessionId"]; // session_id($_COOKIE["sessionId"]); // session_start(); // echo "<br>"; // echo $_SESSION["zh1"]; // echo "<br>"; // echo $_SESSION["pw1"] ?>
<script> <?php // if ($_SESSION["zh1"]) { // echo '$(location).attr("href","234.php");'; // } ?> // setTimeout("")</script></body></html>

123.php

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>21103</title>    <script src="jquery-3.6.0.js"></script>    <script src="jquery.cookie.js"></script>    <script>        $(function(){            $.cookie("sessionId",$(".cc").attr("cc"),{expires:7});            $(".cc").html($.cookie("sessionId"));        })</script></head><body>    处理session和cookie页面    <!-- 1.获取账号密码数据,生成随机session_id,写入session -->    <?php    session_start();    $_SESSION["tx1"] = $_POST["tx1"];    $_SESSION["pw1"] = $_POST["pw1"];    ?>
<script> // 2.给浏览器写入cookie $.cookie("sessionID",<?php echo '"'.session_id().'"' ?>,{expires:9999}); $.cookie("tx1",<?php echo '"'.$_SESSION['tx1'].'"' ?>); $.cookie("pw1",<?php echo '"'.$_SESSION['pw1'].'"' ?>);
setTimeout(function(){ $(location).attr("href","234.php"); },5000);</script>
</body></html>

234.php

<!DOCTYPE html><html lang="en"><head>    <!-- <meta http-equiv="refresh" content="1;url=123.php"> -->    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Document</title></head><body>    自动登录成功</body></html>

7.字符串处理

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>21103</title>    <script src="jquery-3.6.0.js"></script>    <script src="jquery.cookie.js"></script>    <script>        $(function(){            // $("body").html("666");        })</script></head><body>    <?php        $str = "&nbsp;hello world";        echo $str;        echo "<br>";        echo trim($str,"&nbsp;");        echo "<br>";        echo strtoupper($str);        echo "<br>";        echo substr_count($str,"l");        echo "<br>";        echo str_replace("o","a",$str);        echo "<br>";        echo htmlspecialchars("&nbsp;");
?></body></html>

8.自动登录(完善)

index.html

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Document</title>    <script src="jquery-3.6.0.js"></script>    <script src="jquery.cookie.js"></script>    <script>
// 1、判断cookie数据sessionID是否存在,是就判断和服务器session ID一致,是就跳转到登录后234.php //不存在就记录phpsessid到sessionID $(function(){ <?php if($_COOKIE["sessionID"]){ session_id($_COOKIE["sessionID"]); session_start(); if($_SESSION["tx1"]){ echo '$(location).attr("href","234.php");'; } }else{ setcookie("sessionID",$_COOKIE["PHPSESSID"],time()+10000000); // $.cookie("sessionID",$.cookie("PHPSESSID"),{expires : 7}); } ?> })</script></head><body> 这是主页 <form action="123.php" method="POST"> 账号:<input type="text" name="tx1"> <br> 密码:<input type="password" name="pw1"> <br> <input type="submit"> </form></body></html>

123.php

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=, initial-scale=1.0">    <title>Document</title>    <script src="jquery-3.6.0.js"></script>    <script src="jquery.cookie.js"></script>    <script>
// 2、2秒后自动跳转登陆后页面 $(function(){ setTimeout(function(){ $(location).attr("href","234.php"); }, 2000); })</script></head><body> 处理session和cookie页面 <br>
<!-- 获取账号密码数据,生成随机session id,写入session --> <?php session_start(); $_SESSION["tx1"] = $_POST["tx1"]; $_SESSION["pw1"] = $_POST["pw1"]; ?>

</body></html>

234.php

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Document</title></head><body>    登录成功!</body></html>

9.HTML标签输出处理,字符串处理,中文编码,REQUEST_URL

index.html

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Document</title>    <style>        a{            text-decoration: none;            color: black;        }        a:hover{            color: red;        }        button:hover{            border: 1px solid orangered;        }</style></head><body>    <button><a href="123.php/?login">登陆</a></button>    <button><a href="123.php/?nologin">未登陆</a></button>    <br><br>    <button><a href="123.php/?ba=你好&zh=abcd">贴吧</a></button></body></html>

123.php

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Document</title></head><body>    <?php
echo strip_tags("<div>aaaaa</div>"); echo "<br>"; echo substr("qwertyuiop",6,2); echo "<br>"; echo $_SERVER["REQUEST_URI"]; echo "<br>"; $num = strpos($_SERVER["REQUEST_URI"],"?")+1; echo "<br>"; if(substr($_SERVER["REQUEST_URI"],$num) == "nologin") { echo "欢迎游客"; }else if(substr($_SERVER["REQUEST_URI"],$num) == "login"){ echo "欢迎登陆"; } echo "<br>"; // echo var_dump(explode("&",substr($_SERVER["REQUEST_URI"],$num))); // echo var_dump(preg_split("/[=&]+/is",substr($_SERVER["REQUEST_URI"],$num))); $tieba = explode("&",substr($_SERVER["REQUEST_URI"],$num)); echo $tieba[0]; echo "<br>"; echo $tieba[1]; echo "<br>"; echo explode("=",$tieba[0])[1]; print_r(str_split(explode("=",$tieba[0])[1],9)); echo "<br>"; echo str_split(explode("=",$tieba[0])[1],9)[0]; echo "<br>"; echo str_split(explode("=",$tieba[0])[1],9)[1];

?></body></html>

10.正则表达式

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>21103</title></head><body>    <?php
// $pattern = "/qqq/";//匹配三个q // $str = "qqddsffqqqdxvvxaaasswwaqqqassaaafff"; // var_dump(preg_match_all($pattern,$str,$array)); // echo "<br>"; // print_r($array); // echo "<br>";
// $pattern = "/aq*q/";//匹配星号前面字符,任意个,包括0 // $str = "aqqddsaqffqqqdxvvxaaasswwaqqqassaaafff"; // var_dump(preg_match_all($pattern,$str,$array)); // echo "<br>"; // print_r($array); // echo "<br>";
// $pattern = "/aq?q/";//匹配星号前面字符,0或1个 // $str = "aqqddsaqffqqqdxvvxaaasswwaqqqassaaafff"; // var_dump(preg_match_all($pattern,$str,$array)); // echo "<br>"; // print_r($array); // echo "<br>";
// $pattern = "/aq+b/";//匹配星号前面字符,至少一个前面字符 // $str = "abqddsaqbfqqqdxvvxaaasswwaqqqbssaaafff"; // var_dump(preg_match_all($pattern,$str,$array)); // echo "<br>"; // print_r($array); // echo "<br>";
// $pattern = "/a.q/";//匹配a*q,*可以是任意字符,但不能为空 // $str = "abqddsaqbfqqqdxvvxaaasswwaqqqbssaaafffabq"; // var_dump(preg_match_all($pattern,$str,$array)); // echo "<br>"; // print_r($array); // echo "<br>";
// $pattern = "/^abq/";//匹配开头 // $str = "abqddsaqbfqqqdxvvxaaasswwaqqqbssaaafff"; // var_dump(preg_match_all($pattern,$str,$array)); // echo "<br>"; // print_r($array); // echo "<br>";
// $pattern = "/abq$/";//匹配结尾 // $str = "abqddsaqbfqqqdxvvxaaasswwaqqqbssaaafffabq"; // var_dump(preg_match_all($pattern,$str,$array)); // echo "<br>"; // print_r($array); // echo "<br>";
// $pattern = "/aq{2}/";//匹配a+2个q // $str = "aq,aqq,qqq,qqqq"; // var_dump(preg_match_all($pattern,$str,$array)); // echo "<br>"; // print_r($array); // echo "<br>";
// $pattern = "/a[a-f]/";//匹配a+a-f任意字符 // $str = "aqazafawawaaarrf"; // var_dump(preg_match_all($pattern,$str,$array)); // echo "<br>"; // print_r($array); // echo "<br>";
// $pattern = "/ac|ca/";//匹配ac或ca // $str = "acqazafawawaaarrcaf"; // var_dump(preg_match_all($pattern,$str,$array)); // echo "<br>"; // print_r($array); // echo "<br>";
// $pattern = "/ac|ca/i";//匹配ac或ca(i:忽略大小写) // $str = "acqazAcafawawaCAaarrACcaf"; // var_dump(preg_match_all($pattern,$str,$array)); // echo "<br>"; // print_r($array); // echo "<br>";
// $pattern = "/ac|\n/is";//匹配ac或ca(i:忽略大小写,s换行符) // $str = "acqazAcafaw\nawaCAaarrACcaf"; // var_dump(preg_match_all($pattern,$str,$array)); // echo "<br>"; // print_r($array); // echo "<br>";
// $pattern = "/\d{3}/";//匹配3位数字 // $str = "1,12,123,1234,a123,aq12"; // var_dump(preg_match_all($pattern,$str,$array)); // echo "<br>"; // print_r($array); // echo "<br>";
// $pattern = "/\D{3}/";//匹配3位非数字 // $str = "1,12,123,1234,a123,aq12aaa"; // var_dump(preg_match_all($pattern,$str,$array)); // echo "<br>"; // print_r($array); // echo "<br>";
// $pattern = "/er\b/";//匹配er结尾,以空格为界限 // $str = "never,verb"; // var_dump(preg_match_all($pattern,$str,$array)); // echo "<br>"; // print_r($array); // echo "<br>";
// $pattern = "/er\B/";//匹配非er结尾,以空格为界限 // $str = "never,verb"; // var_dump(preg_match_all($pattern,$str,$array)); // echo "<br>"; // print_r($array); // echo "<br>";
$pattern = "/\w+/";//单个单词为数组元素 $str = "never,verb"; var_dump(preg_match_all($pattern,$str,$array)); echo "<br>"; print_r($array); echo "<br>";
?></body></html>

11.正则表达式(续)

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>211</title>
</head><body> <?php
// $pattern = "/[\x{4e00}-\x{9fa5}]/u"; // // $pattern = "/[一-龥]/u"; // $str = "ab2d一ef的3都是f"; // echo preg_match_all($pattern,$str,$arr)."<br>"; // print_r($arr);
// $pattern = "/[\x{4e00}-\x{9fa5}]{1,}/u"; // $str = "a是gf方便2d一并不比efg的个对付3都是突然让他f"; // echo preg_match_all($pattern,$str,$arr)."<br>"; // print_r($arr);
// $pattern = "/[\x{4e00}-\x{9fa5}]{1,3}/u"; // $str = "a是gf方便2d一并不比efg的个对付3都是突然让他f"; // echo preg_match_all($pattern,$str,$arr)."<br>"; // print_r($arr);
// $pattern = "/[\x{4e00}-\x{9fa5}]{2,3}/u"; // $str = "a是gf方便2d一并不比efg的个对付3都是突然让他f"; // echo preg_match_all($pattern,$str,$arr)."<br>"; // print_r($arr);
// $pattern = "/(ab){2}/"; // $str = "ab,aab,abb,aabb,abab"; // echo preg_match_all($pattern,$str,$arr)."<br>"; // print_r($arr);
// $pattern = "/(?:ab){2}/"; // $str = "ab,aab,abb,aabb,abab"; // echo preg_match_all($pattern,$str,$arr)."<br>"; // print_r($arr);
// $pattern = "/[0-9]+(?=f)/"; // $str = "dgnkl999knvdsnb888fhdfgb"; // echo preg_match_all($pattern,$str,$arr)."<br>"; // print_r($arr);
// $pattern = "/(?<=b)[0-9]+/"; // $str = "dgnkl999knvdsnb888fhdfgb"; // echo preg_match_all($pattern,$str,$arr)."<br>"; // print_r($arr);
// $pattern = "/(?<!b)[0-9]+/"; // $str = "dgnkl999knvdsnb888fhdfgb"; // echo preg_match_all($pattern,$str,$arr)."<br>"; // print_r($arr);
// $pattern = "/[0-9]+(?!=b)/"; // $str = "dgnkl999knvdsnb888fhdfgb"; // echo preg_match_all($pattern,$str,$arr)."<br>"; // print_r($arr);
$pattern = "/收集了(?:[0-9]+|[\x{4e00}-\x{9fa5}]+|[a-z]+)个石头/u"; $str = "收集了6个石头dfbddggb收集了660个石头sfjmglnnlk收集了六个石头sdfnhj收集了six个石头"; echo preg_match_all($pattern,$str,$arr)."<br>"; print_r($arr);
?></body></html>

12.正则表达式(再续),字符串替换,匹配

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>21103</title></head><body>    <?php
// $str = "sjhhfsdgdg@dhh㴳sh"; // $pattern = "/\x{3d33}/u"; // preg_match_all($pattern,$str,$array); // print_r($array);
// $str = "sjhhfsd©gdg@dhh㴳sh"; // $pattern = "/\u{3d33}/"; // preg_match_all($pattern,$str,$array); // print_r($array);
// $str = "sjhhfsd©gdg@dhh㴳sh"; // $pattern = "/\u{00a9}/"; // preg_match_all($pattern,$str,$array); // print_r($array);
// $str = "divsggsggadivdivshdbnahnadiv"; // $pattern = "/div.+adiv/U"; // preg_match_all($pattern,$str,$array); // print_r($array);
// $str = "divs111gsggad444ivdivshdb666nahnadiv"; // $pattern = "/\d{3}/"; // $newstr = preg_replace($pattern,"11111",$str); // print_r($newstr); // echo "<hr>"; // $str = "&emsp;divs111gsggad444ivdivshdb666nahnadiv"; // echo $str."<br>"; // $pattern = "/\t/"; // preg_match_all($pattern,$str,$array); // print_r($array); // echo "<hr>";
// $str = "divs\t111gsggad\t444iv\tdivshdb\t666nahnadiv"; // echo $str."<br>"; // $pattern = "/\t/"; // preg_match_all($pattern,$str,$array); // print_r($array); // echo "<hr>";

// $str = "helloo"; // $pattern = "/(.)\1/"; // echo preg_match($pattern, $str); // if (preg_match($pattern, $str)) { // echo "字符串中包含两个连续相同的字符"; // } else { // echo "字符串中不包含两个连续相同的字符"; // } // echo "<hr>";
// $str1 = "good"; // $str2 = "book"; // $str3 = "apple"; // $pattern = "/(.)\1/"; // preg_match($pattern, $str1, $matches1); // preg_match($pattern, $str2, $matches2); // preg_match($pattern, $str3, $matches3); // print_r($matches1); // Array([0] => oo, [1] => o) // print_r($matches2); // Array([0] => oo, [1] => o) // print_r($matches3); // Array([0] => pp, [1] => p) // echo "<hr>";
// $str = "好好学习,天天向上"; // $pattern = "/(.)\1/u"; // preg_match_all($pattern, $str, $matches); // print_r($matches); // echo "<hr>";
// $str1 = "good"; // $str2 = "book"; // $str3 = "apple"; // $pattern = "/(.)\1/"; // preg_match($pattern, $str1, $matches1); // preg_match($pattern, $str2, $matches2); // preg_match($pattern, $str3, $matches3); // var_dump($matches1); // var_dump($matches2); // var_dump($matches3); // $str = "Hello, world!"; // $pattern = "/(\w+)/"; // $count = 0;
// $new_str = preg_replace_callback($pattern, function ($matches) use (&$count) { // $count++; // return strtoupper($matches[0]); // }, $str, -1, $count);
// echo "原字符串:$str <br>"; // echo "新字符串:$new_str <br>"; // echo "共替换了 $count 个单词。";
$str = "good book apple ooxxpp"; $pattern = "/(.)\1/";
preg_match_all($pattern, $str, $matches);
echo "原字符串:$str <br>"; echo "匹配结果:"; if (empty($matches[0])) { echo "没有匹配到任何连续字符。"; } else { print_r($matches[0]); }



?>
<script> var str ="aqasddfawa"; console.log(str.match(/a.a/g)); console.log(str.match(/a.*?a/)); </script>
</body></html>

13.正则表达式(再再续),localstorage本地存储

index.php

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Document</title></head><body>    <?php        $str = "dfbsd123bfgzd344fhfbh554fdffhdfh";        $pattern = "/[0-9]{3}/";        $replace = "aaa";        echo $str;        echo "<br>";        echo preg_replace($pattern,$replace,$str)."<hr>";
$str = "dfbsd123bfgzd344fhfbh554fdffhdfh"; $pattern = "/\w{3}(\d{3})/"; $replace = "aaa$1"; echo $str; echo "<br>"; echo preg_replace($pattern,$replace,$str)."<hr>";
$str = "dfbsd123bfgzd344fhfbh554fdffhdfh"; $pattern = "/\w{3}(\d{3})/"; echo $str; echo "<br>"; echo preg_replace_callback($pattern,"fun1",$str); function fun1($matches){ print_r($matches); echo "<br>"; return "aaa".($matches[1]+100); } ?>
<input class="in1" type="text"> <input class="bt1" type="button" value="add"> <input class="bt2" type="button" value="read"> <input class="bt3" type="button" value="remove"> <input class="bt4" type="button" value="clear"> <script src="jquery-3.6.0.js"></script> <script> var num = localStorage.getItem("num"); $(".bt1").on("click",function(){ console.log($(".in1").val()); num++; localStorage.setItem("in"+num,$(".in1").val()); localStorage.setItem("num",num); }); $(".bt2").on("click",function(){ $(".in1").val(localStorage.getItem("in"+num)); }); $(".bt3").on("click",function(){ localStorage.removeItem("in"+num); num--; localStorage.setItem("num",num); }); $(".bt4").on("click",function(){ localStorage.clear(); });</script></body></html>

1.html

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>21103</title>    <script src="jquery-3.6.0.js"></script></head><body>    <input class="in1" type="text">    <input class="bt1" type="button" value="add">    <input class="bt2" type="button" value="read">    <input class="bt3" type="button" value="remove">    <input class="bt4" type="button" value="clear">    <script>        var num = localStorage.getItem("num");        $(".bt1").on("click",function(){            console.log($(".in1").val());            num++;            localStorage.setItem("in"+num,$(".in1").val());            localStorage.setItem("num",num);        });        $(".bt2").on("click",function(){            $(".in1").val(localStorage.getItem("in"+num));        });        $(".bt3").on("click",function(){            localStorage.removeItem("in"+num);            num--;            localStorage.setItem("num",num);        });        $(".bt4").on("click",function(){            localStorage.clear();        });</script></body></html>

14.验证码

index.php

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>21103</title></head><body>    <input type="text">    <img src="yanzhengma.php" alt="">    <button>刷新验证码</button>    <button>提交</button></body></html>

yanzhengma.php

<?php    header("Content-type: image/png");    // 面板宽高    $width = 200;    $height = 60;    // 设置面板    $image1 = imagecreatetruecolor($width,$height);    // 设置背景颜色    $colorRed = imagecolorallocate($image1,255,0,0);    $colorGreen = imagecolorallocate($image1,0,255,0);    $colorBlue = imagecolorallocate($image1,0,0,255);    $colorBlack = imagecolorallocate($image1,0,0,0);    $colorBackground = imagecolorallocate($image1,rand(200,255),rand(200,255),rand(200,255));    // 填充背景颜色    imagefill($image1,0,0,$colorBackground);    // 设置图片外框    imagerectangle($image1,0,0,$width-1,$height-1,$colorBlack);    // 随机100个干扰点    for($i=0;$i<100;$i++){        $colorPixel = imagecolorallocate($image1,rand(0,100),rand(0,100),rand(0,100));        imagesetPixel($image1,rand(0,$width-1),rand(0,$height-1),$colorPixel);    }    // 随机干扰线    for($i=0;$i<3;$i++){        $colorLine = imagecolorallocate($image1,rand(0,100),rand(0,100),rand(0,100));        imageline($image1,rand(0,$width/3),rand(0,$height),rand(($width*2)/3,$width-1),rand(0,$height-1),$colorLine);    }    // 输出图片    imagepng($image1);    // 关闭图片编辑器    imagedestroy($image1);

?>

相关推荐