如果字符串中的字符超过 160,则显示警告
Show a warning if characters are more then 160 in a string
我想实现一个像 Twitter 这样的 post 更新系统,用户可以在其中以 160 个字符更新他们的状态。我想添加一些限制,例如如果用户输入的字符超过 160 个,则 update_post.php 文件必须显示 him/her 警告和额外字符(+160 ) 在 HTML del 标记内。 **波纹管是我到目前为止尝试过的代码。但它什么也没输出!**非常感谢任何帮助!谢谢
sample_update.php
<form action="<?php echo $_SERVER['PHP_SELF'];?>"method="post">
<textarea name="msg"></textarea>
<input type="submit"value="Post">
</form>
<?php
if(strlen($txt)>160) {
echo "Your post couldn't be submitted as it contains more then 160 chrecters!";
$txt=$_POST['msg'];
$checking=substr($txt,160);
echo "<del style='color:red;'>$checking</del>";
}
?>
$txt
设置在您的 if
语句中,您需要将其移到
之外
$txt=$_POST['msg'];
if(strlen($txt)>160)
{
echo "Your post couldn't be submitted as it contains more then 160 chrecters!";
$checking=substr($txt,160);
echo "<del style='color:red;'>$checking</del>";
}
这应该适合你:
($_SERVER['SELF']
不存在仅 $_SERVER['PHP_SELF']
另外你必须先赋值变量才能检查长度)
<form action="<?= $_SERVER['PHP_SELF'];?>"method="post">
<textarea name="msg"></textarea>
<input type="submit"value="Post">
</form>
<?php
if(!empty($_POST['msg'])) {
$txt = $_POST['msg'];
if(strlen($txt) > 160) {
echo "Your post couldn't be submitted as it contains more then 160 chrecters!";
$checking = substr($txt,160);
echo "<del style='color:red;'>$checking</del>";
}
}
?>
您应该收到有关未定义变量的通知。从 I/we 可以看出,这是 $txt
,$txt
是在您的 if 循环中定义的。我已将您的代码修改为最少的行,但同样有效。
if (isset($_POST['msg'])){
if (strlen($_POST['msg']) > 160){
echo "Your post could not be submitted as it contains more than 160 characters!";
echo "<del style='color:red;'>".substr($_POST['msg'],160)."</del>";
}
}
我还用 isset
语句包裹了你的 $_POST,它会在执行任何其他操作之前检查它是否已设置。如果未设置任何内容,则代码将不会执行并触发一些烦人的错误消息
首先你必须使用$_SERVER['PHP_SELF']
而不是$_SERVER['SELF']
您可能想提高您的一些条件,这样您就可以将支票用于其他用途。此外,将用户键入的文本插入文本区域是一种很好的做法,因此用户不必再次重新键入文本。
<?php
$maxlen = 160;
$txt=(isset($_POST['msg'])) ? $_POST['msg'] : "";
$check = strlen($txt) > $maxlen;
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<textarea name="msg"><?php echo $txt; ?></textarea>
<input type="submit" value="post">
</form>
<?php
if ($check){
echo "Your post couldn't be submitted as it contains more then $maxlen chrecters!";
$checking = substr($txt,$maxlen);
echo "<del style='color:red;'>$checking</del>";
} else {
echo "You are good to go ma man - do something";
}
?>
我想实现一个像 Twitter 这样的 post 更新系统,用户可以在其中以 160 个字符更新他们的状态。我想添加一些限制,例如如果用户输入的字符超过 160 个,则 update_post.php 文件必须显示 him/her 警告和额外字符(+160 ) 在 HTML del 标记内。 **波纹管是我到目前为止尝试过的代码。但它什么也没输出!**非常感谢任何帮助!谢谢
sample_update.php
<form action="<?php echo $_SERVER['PHP_SELF'];?>"method="post">
<textarea name="msg"></textarea>
<input type="submit"value="Post">
</form>
<?php
if(strlen($txt)>160) {
echo "Your post couldn't be submitted as it contains more then 160 chrecters!";
$txt=$_POST['msg'];
$checking=substr($txt,160);
echo "<del style='color:red;'>$checking</del>";
}
?>
$txt
设置在您的 if
语句中,您需要将其移到
$txt=$_POST['msg'];
if(strlen($txt)>160)
{
echo "Your post couldn't be submitted as it contains more then 160 chrecters!";
$checking=substr($txt,160);
echo "<del style='color:red;'>$checking</del>";
}
这应该适合你:
($_SERVER['SELF']
不存在仅 $_SERVER['PHP_SELF']
另外你必须先赋值变量才能检查长度)
<form action="<?= $_SERVER['PHP_SELF'];?>"method="post">
<textarea name="msg"></textarea>
<input type="submit"value="Post">
</form>
<?php
if(!empty($_POST['msg'])) {
$txt = $_POST['msg'];
if(strlen($txt) > 160) {
echo "Your post couldn't be submitted as it contains more then 160 chrecters!";
$checking = substr($txt,160);
echo "<del style='color:red;'>$checking</del>";
}
}
?>
您应该收到有关未定义变量的通知。从 I/we 可以看出,这是 $txt
,$txt
是在您的 if 循环中定义的。我已将您的代码修改为最少的行,但同样有效。
if (isset($_POST['msg'])){
if (strlen($_POST['msg']) > 160){
echo "Your post could not be submitted as it contains more than 160 characters!";
echo "<del style='color:red;'>".substr($_POST['msg'],160)."</del>";
}
}
我还用 isset
语句包裹了你的 $_POST,它会在执行任何其他操作之前检查它是否已设置。如果未设置任何内容,则代码将不会执行并触发一些烦人的错误消息
首先你必须使用$_SERVER['PHP_SELF']
而不是$_SERVER['SELF']
您可能想提高您的一些条件,这样您就可以将支票用于其他用途。此外,将用户键入的文本插入文本区域是一种很好的做法,因此用户不必再次重新键入文本。
<?php
$maxlen = 160;
$txt=(isset($_POST['msg'])) ? $_POST['msg'] : "";
$check = strlen($txt) > $maxlen;
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<textarea name="msg"><?php echo $txt; ?></textarea>
<input type="submit" value="post">
</form>
<?php
if ($check){
echo "Your post couldn't be submitted as it contains more then $maxlen chrecters!";
$checking = substr($txt,$maxlen);
echo "<del style='color:red;'>$checking</del>";
} else {
echo "You are good to go ma man - do something";
}
?>