'i want to echo this string but its not working [closed]

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


Solution 1:[1]

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

Solution 2:[2]

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

Solution 3:[3]

$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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 waterloomatt
Solution 2 Cris
Solution 3