在PHP中,可以使用以下步骤来确保输入到表单中的句子至少有20个字符,并将最后9个字符拉到新的页面上显示:
以下是一个示例代码:
<?php
if(isset($_POST['submit'])){
$sentence = $_POST['sentence'];
if(strlen($sentence) < 20){
$last_chars = substr($sentence, -9);
header("Location: new_page.php?last_chars=$last_chars");
exit();
}
}
?>
<form method="POST" action="">
<label for="sentence">请输入句子:</label>
<input type="text" name="sentence" id="sentence" required>
<input type="submit" name="submit" value="提交">
</form>
在新的页面new_page.php中,可以使用以下代码来显示最后9个字符:
<?php
if(isset($_GET['last_chars'])){
$last_chars = $_GET['last_chars'];
echo "最后9个字符是:$last_chars";
}
?>
这样,当用户输入的句子长度小于20个字符时,会自动跳转到新的页面,并显示最后9个字符。
领取专属 10元无门槛券
手把手带您无忧上云