Home > Backend Development > PHP Tutorial > PHP中如何控制一个方法的字符串类型参数长度小于等于255字节?

PHP中如何控制一个方法的字符串类型参数长度小于等于255字节?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:10:42
Original
1112 people have browsed it

注意是字节哦

回复内容:

注意是字节哦

//直接报错
function test($str) {
    if (!is_string($str) or strlen($str) > 255) {
        throw new Exception('not a string or too long');
    }
}

//自动截断
function test2($str) {
    if (!is_string($str)) {
        throw new Exception('not a string');
    }
    $str = substr($str, 0, 255); //php的substr是二进制安全的
}
Copy after login

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template