substr using php
We want your help to write a function to add +1 to fetched results by MYQSL, we are currently getting max row (if there is else NULL) from mysql in format of 2011/345, so we want to post it back again after +1 to last part means 345+1 so f开发者_运维百科inal string should be 2011/346 to insert. How can we write this function in php and should substr both 2011 and 345, Please help, thanks.
$rows = '2011/345';
if($rows){
$parts = explode('/', $rows);
(int)$parts[1] += 1;
$return = implode('/', $parts);
echo $return;
}
Should do it :)
Edit: Fixed some bugs :)
Edit2: Considered NULL result
Just shorter version of @ayk code
$rows = '2011/345';
if($rows){
list($a,$b) = explode('/', $rows);
echo $a.'/'.++$b;
}
<?php
$data = "2011/345";
$lastNumber = end(explode("/",$data));
$len = strlen($lastNumber);
$lastNumber++;
$data = substr($data,0,0-$len).$lastNumber;
echo $data; // your new value
?>
精彩评论