首页 > 解决方案 > 更改文本 PHP

问题描述

我必须更改字符串“游戏规则:您必须以最少的次数猜测从系统中随机抽取的 0 到 99 之间的数字。” 仅在满足条件时才显示“输入的数字太小”。因此,必须将字符串替换在初始字符串所在的相同位置。我怎样才能改变它?我已经尝试过 str_replace 和 preg_replace 但它不起作用。

此外,每次用户猜错数字时,我都必须增加变量 x ,但我不知道如何。

<?php
    $indovina=7;
    $x = 0;
    $num=$_GET['num1'];
    if ($num<$indovina){
        echo "Il numero inserito e troppo piccolo";
    }
    else if ($num>$indovina){
        echo "Il numero inserito e' troppo grande";
    }
    if ($num==$indovina){
        echo "Bravo, hai indovinato";
    }
?>



<html>

    <head>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
    </head>
    <body>
    <br><br>
    <center>
    <div class="card" style="width: 40rem;">
        <div class="card-body">
        <center>
            <font size=5>Gioco dell' Indovina Numero</font><br>
        </center>
        <div id="risultato" name="risultato">
        <center>
            <label size=3><b>Regole del gioco:</b> Si deve indovinare nel minor numero di tentativi un <br> numero compreso fra 0 e 99 estratto casualmente dal sistema.</label>
        </center>
        </div>
        <br>
        <form method="GET">
        <label id=tentativo>Tentativo n.1</label>
        <input type="number" id="num1" name="num1">
        <button type="button" class="btn btn-primary">Tenta</button>
        </form>
        </div>
    </div>
    <center>
    </body>
    
</html>

标签: phphtmlcssstringstr-replace

解决方案


echo 语句将在代码中的位置输出字符串

所以你需要在标签内回显它,我做了一个例子,如果没有提供数字,我会在正确的地方回显并写出标准文本(初始)

<head>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
</head>
<body>
<br><br>
<center>
<div class="card" style="width: 40rem;">
    <div class="card-body">
    <center>
        <font size=5>Gioco dell' Indovina Numero</font><br>
    </center>
    <div id="risultato" name="risultato">
    <center>
        <label>
        <?php
            $indovina=7;
            $x = 0;
            $num=array_key_exists('num1', $_GET) ? $_GET['num1'] : false;
            if(!$num){
                echo "<b>Regole del gioco:</b> Si deve indovinare nel minor numero di tentativi un <br> numero compreso fra 0 e 99 estratto casualmente dal sistema.";
            }else{
                if ($num<$indovina){
                    echo "Il numero inserito e troppo piccolo";
                }
                else if ($num>$indovina){
                    echo "Il numero inserito e' troppo grande";
                }
                if ($num==$indovina){
                    echo "Bravo, hai indovinato";
                }    
            }
        ?>

        </label>
    </center>
    </div>
    <br>
    <form method="GET">
    <label id=tentativo>Tentativo n.1</label>
    <input type="number" id="num1" name="num1">
    <button type="button" class="btn btn-primary">Tenta</button>
    </form>
    </div>
</div>
<center>
</body>

x 和 y 之间的随机数是一次互联网搜索,要增加 x,您必须增加它并将其保存在例如隐藏输入中,并且每次检索“last x”以进行增加


推荐阅读