[英]How to get the last three digit before a character in PHP
我正在嘗試使功能Makecode()
代碼必須看起來像這樣
SV00000001-15
要創建這個,我必須這樣做。
function makeCode()
{
$counter=000;
$buf = 00000;
$suf= $this->suf;
$pre= $this-pref;
$counter = addCount($this->lastCode);
$code = $pref.$buf.$counter."-".$suf;
}
function addCount($code)
{
//I have to know how to get the last three digit before"-" and then return a number one greater than that .
}
在上述情況下, $this-lastCOde = 'SV00000001-15'
一些更多的解釋
對於功能addCount()輸入將為SV00000001-15
,預期輸出為002
我需要輸出為002
即我需要先獲取"-"
之前的最后三位數字,然后再遞增一位,請注意,返回值必須為三位數字格式
如果我的問題不清楚,請在下面評論
感謝和問候
所以我必須要
從字符串中獲取最后三個字符,例如
substr("SV00000001-15", -3); // returns "-15"
要么
$no = substr(substr("SV00000001-15",0,strrpos("SV00000001-15","-")),-3); // returns "001"
echo sprintf('%03d', (int)$no + 1); //returns 002
要么
$no = substr(explode("-","SV00000001-15")[0],-3); //returns 001
echo sprintf('%03d', (int)$no + 1); //returns 002
在你的職能
function addCount($code)
{
$no = substr(substr($code,0,strrpos($code,"-")),-3); // returns "001"
echo sprintf('%03d', (int)$no + 1); //returns 002
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.