如果字符串中的字符数超过160,则显示警告

I want to implement a post update system like twitter where users can update their status in 160 charecters. I want to add some restrictions like If a user entered characters more then 160 ,then the update_post.php file must show him/her a warning and the extra characters(+160) inside HTML del tag. **Bellow is my code that I have tried so far. but it outputs nothing!**Any help is much appriciated! thanks

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 is set inside of your if statement you need to move it outside

$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>";
}

This should work for you:

($_SERVER['SELF'] doesn't exists only $_SERVER['PHP_SELF'] Also you have to first assign the variable before you can check the length)

<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>";
        }
    } 



?>

You should be getting notices about an undefined variable. This being $txt as from what I/we can see, $txt is defined inside your if loop. I have modified your code to be minimal lines but just as effective.

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>";
    }

}

I have also wrapped your $_POST around an isset statement, which will check if it's set before doing anything else.. If nothing is set, then the code will not execute and trigger some annoying error messages

First of all you have to use $_SERVER['PHP_SELF'] instead of $_SERVER['SELF']

You might want to move some of your conditions up, so you can use the check for something else. Furthermore, inserting the user typed text into the textarea is a good practice so the user dosnt have to retype the text again.

<?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";
}
?>