开发者

Show an image if the character count of a data field is equal to a specific number of characters

开发者 https://www.devze.com 2023-02-06 13:30 出处:网络
I have a field in which all data needs to be 13 characters in length.I would like to show a check mark image if the file name that was placed in the database is the correct number of characters (13) o

I have a field in which all data needs to be 13 characters in length. I would like to show a check mark image if the file name that was placed in the database is the correct number of characters (13) or show an exclamation mark image if the file na开发者_运维知识库me is not equal to 13 characters in length. This is what I have so far, but obviously it's not working.

<?php
$val1 = 13;
if (count_chars($image_id) == ($val1)) {
echo '<img src="images/icons/check.gif" />';
}
else {
echo '<img src="images/icons/exclamation.gif" />';
}
?>


Use strlen(). count_chars() is for counting the number of occurances of each character in the alphabet in a string.


<?php
  $val1 = 13;
  if (strlen($image_id) == ($val1)) {
    echo '<img src="images/icons/check.gif" />';
  }
  else {
    echo '<img src="images/icons/exclamation.gif" />';
  }
?>


$val = '<img src="images/icons/exclamation.gif" />';

if(strlen($image_id) == 13)
{
    $val = '<img src="images/icons/check.gif" />';
}

echo $val

Firstly you can save some resources by not using the else statement as the above works the exact same whay.

Secondly you only need to assign the integer of 13 to a variable if your going to use it over and over, you can just do if(strlen($a) == 13)

http://php.net/manual/en/function.count-chars.php stats that:

mixed count_chars ( string $string [, int $mode = 0 ] )

Counts the number of occurrences of every byte-value (0..255) in string and returns it in various ways.

where as strlen returns the length of the given string.

0

精彩评论

暂无评论...
验证码 换一张
取 消