I want to add number from my string. this is my string
$formula = 'IF(B15="","",IF(ISNUMBER(A14),A14+1,IF(ISNUMBER(B3),B3+1,1)))';
i want add number if before the number is alphabet. for example i want to make B15 to B16 ini my string.. this my loop
$jum = strlen($formula);
for($i = 0; $i < $jum; $i++){
if(ctype_alpha($formula[$i]) && ctype_alnum($formula[$i+1])){
$temp_formula[$i+1] += 1;
}
}
You can do this with preg_replace_callback
. Use this regex, which looks for a symbol of the form <letter><digits>
between word boundaries:
\b([A-Z])(\d+)\b
and then in the callback the second group's value can be incremented:
$formula = 'IF(B15="","",IF(ISNUMBER(A14),A14+1,IF(ISNUMBER(B3),B3+1,1)))';
echo preg_replace_callback('/\b([A-Z])(\d+)\b/', function ($m) {
return $m[1] . (1 + $m[2]);
}, $formula);
Output:
IF(B16="","",IF(ISNUMBER(A15),A15+1,IF(ISNUMBER(B4),B4+1,1)))
Update
To use an external variable in the callback (as described in the comments), edit the code like this:
$formula = 'IF(B15="","",IF(ISNUMBER(A14),A14+1,IF(ISNUMBER(B3),B3+1,1)))';
$tambah = $adding + ($rekord*$highest);
echo preg_replace_callback('/\b([A-Z])(\d+)\b/', function ($m) use ($tambah) {
return $m[1] . ($tambah + $m[2]);
}, $formula);