Home > Mobile >  i want to echo this string but its not working
i want to echo this string but its not working

Time:05-14

I intend to echo the string if the string isn't longer than 20 characters. But it's not working even though the string isn't longer than 20 characters.

$Title = "this is a string";
    if (!mb_strimwidth($Title, 0, 20)) {
        echo $Title;
      }
      else {
        header("google.com");
      }

CodePudding user response:

You can just use mb_strlen or strlen for this.

<?php

$title = 'this is a string';

if (mb_strlen($title) === 20) {
    echo 'The string string is 20 characters long';
} else {
    echo 'The string string is not 20 characters long';
}

Demo, https://onlinephp.io/c/495a6

CodePudding user response:

mb_strimwidth get a truncated string with specified widht.

If you want evaluate the string long, you can use the function strlen.

For example:

$Title = "this is a string that is 20 characters long";
if (strlen($Title)<=20) {
    echo $Title; //This impression
  } else {
    header("google.com");
  }

Now, if you want evaluate if the string is exactly 20 characters, then

$Title = "this is a string that is 20 characters long";
if (strlen($Title)==20) {
    echo $Title;
  } else {
    header("google.com");
  }

CodePudding user response:

$Title = "this is a string that is 20 characters long";
    if (!strlen($Title) > 20) {
        echo $Title;
      } else {
        header("google.com");
      }

If you need to just get string char count use strlen https://www.php.net/manual/en/function.strlen.php

  • Related