image_hwstring()
云策文档标注
概述
image_hwstring() 函数用于根据给定的宽度和高度值生成 HTML 的 width 和 height 属性字符串。它接受整数或字符串参数,但仅处理数值,并自动去除非数字部分如 'px'。
关键要点
- 函数接受两个必需参数:$width 和 $height,类型为 int 或 string,表示图像的像素尺寸。
- 参数在逻辑上必需,但如果设置为 false 或 null,则不会在返回字符串中添加对应属性。
- 返回值为包含 width 和/或 height 属性的 HTML 字符串,例如 'width="100" height="200"'。
- 函数内部使用 (int) 强制转换参数,确保输出为整数值。
代码示例
function image_hwstring( $width, $height ) {
$out = '';
if ( $width ) {
$out .= 'width="' . (int) $width . '" ';
}
if ( $height ) {
$out .= 'height="' . (int) $height . '" ';
}
return $out;
}注意事项
- 参数值应为数字,字符串中的非数字部分(如 'px')会被自动去除。
- 如果 $width 或 $height 为 false 或 null,则不会生成对应的属性,这允许灵活控制输出。
原文内容
Retrieves width and height attributes using given width and height values.
Description
Both attributes are required in the sense that both parameters must have a value, but are optional in that if you set them to false or null, then they will not be added to the returned string.
You can set the value using a string, but it will only take numeric values.
If you wish to put ‘px’ after the numbers, then it will be stripped out of the return.
Parameters
$widthint|stringrequired-
Image width in pixels.
$heightint|stringrequired-
Image height in pixels.
Source
function image_hwstring( $width, $height ) {
$out = '';
if ( $width ) {
$out .= 'width="' . (int) $width . '" ';
}
if ( $height ) {
$out .= 'height="' . (int) $height . '" ';
}
return $out;
}
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |