简体   繁体   English

PHP的strlen范围功能

[英]php strlen range function

Im trying to write an if function which checks the width of an image, and then apply a css class. 我试图编写一个if函数来检查图像的宽度,然后应用css类。

I want the function to work like this 我希望该功能像这样工作

if image is in an range from 150px to 189px, apply css class "span-4" 如果图片的范围是150像素到189像素,则应用CSS类“ span-4”

190px to 229px: css class "span-5" 190像素到229像素:css类“ span-5”

230px to 269px: css class "span-6" 230像素到269像素:css类“ span-6”

I have tried like this: 我试过这样的:

list($width, $height, $type, $attr) = getimagesize($article_image);

if(strlen($width) < 189 && strlen($width) > 150) { $cssClass = "span-4"; }
if(strlen($width) < 229 && strlen($width) > 190) { $cssClass = "span-5"; }
if(strlen($width) < 269 && strlen($width) > 230) { $cssClass = "span-6"; }

That does not work. 那行不通。 Do anyone see what Im doing wrong? 有人看到我在做什么错吗?

Edit: Added the function to explaine where Im getteing the $width variable from 编辑:添加了功能来解释我从哪里获取$ width变量

It's probably because you're treating $width like a string, and getting its length. 可能是因为您将$width当作一个字符串,并获取了它的长度。 Try treating it like a number instead. 尝试像对待数字一样对待它。

if(($width < 189) && ($width > 150)) { $cssClass = "span-4"; }

strlen gets the string length of the $width variable. strlen获取$ width变量的字符串长度。 I don't think you want to do that. 我想你不想那样做。 You most probably want to lose strlen() from every $width. 您很可能想从每个$ width丢失strlen()。

first of all calculate the string's length only once and put it into a variable. 首先只计算一次字符串的长度,然后将其放入变量中。 Secondly use else if on the second and third statements. 其次,在第二和第三条语句上使用else。

strlen is, like the function name indicate, for strings. 像函数名称所示, strlen是字符串。

I'm not sure I correctly understood what you want to do, but if you have an image file and you want to check it's size, you can use getimagesize . 我不确定我是否正确理解了您要做什么,但是如果您有图像文件并且想要检查其大小,则可以使用getimagesize

If you already have the image's width in the $width variable, you must do your checks like this : 如果您已经在$width变量中包含了图像的宽度,则必须执行以下检查:

if($width > 150 && $width < 190) { $cssClass = "span-4"; }
else if($width < 230) { $cssClass = "span-5"; }
else if($width < 270) { $cssClass = "span-6"; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM