Check string length in PHP

前端 未结 7 1723
渐次进展
渐次进展 2020-11-28 14:28

I have a string that is 141 characters in length. Using the following code I have an if statement to return a message if the string is greater or less than 140.

相关标签:
7条回答
  • 2020-11-28 14:31

    $message is propably not a string at all, but an array. Use $message[0] to access the first element.

    0 讨论(0)
  • 2020-11-28 14:39

    The xpath() function does not return a string. It returns an array with XML elements (of type SimpleXMLElement), which may be casted to a string.

    if (count($message)) {
       if (strlen((string)$message[0]) < 141) {
          echo "There Are No Contests.";
       }
       else if(strlen((string)$message[0]) > 142) {
          echo "There is One Active Contest.";
       }
    }
    
    0 讨论(0)
  • 2020-11-28 14:40

    [0]=> string(141) means that $message is an array, not string, and $message[0] is a string with 141 characters in length.

    0 讨论(0)
  • 2020-11-28 14:42

    [0]=> string(141) means $message is an array so you should do strlen($message[0]) < 141 ...

    0 讨论(0)
  • 2020-11-28 14:42

    Because $xml->xpath always return an array, and strlen expects a string.

    0 讨论(0)
  • 2020-11-28 14:45

    Try the common syntax instead:

    if (strlen($message)<140) {
        echo "less than 140";
    }
    else
        if (strlen($message)>140) {
            echo "more than 140";
        }
        else {
            echo "exactly 140";
        }
    
    0 讨论(0)
提交回复
热议问题