Home >Backend Development >PHP Tutorial >substr截取问题,我需要倒过来第16位后的字符

substr截取问题,我需要倒过来第16位后的字符

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-20 12:43:562069browse

比如字符串为:ES904B1234567812345678或ES904BC1234567812345678
我要取:ES904B或ES904BC

ES904B位数会变,有可能是ES904BC,但后面绝对是16位

怎么用substr截取啊。


回复讨论(解决方案)

substr(0,strlen($str)-16);

substr(str,0,instr(str,'1')-1)

echo substr('ES904B1234567812345678', 0, -16), '<br>';echo substr('ES904BC1234567812345678', 0, -16);
ES904B
ES904BC

substr(0,strlen($str)-16);



不行啊,

echo substr('ES904B1234567812345678', 0, -16), '<br>';echo substr('ES904BC1234567812345678', 0, -16);
ES904B
ES904BC



再请教一个问题。我要取 倒回来8位后的8位数呢。
比如:ES904BC1234567887951543或ES904B1234567887951543
要取12345678

echo substr('ES904BC1234567887951543', -16, 8);
12345678

substr 的第二个参数指示截取开始的位置
substr 的第三个参数指示截取的长度
由于长度不可能是负数,所以 substr 对负数值做了另外的解释:实际取其与字符串长度的和

Statement:
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