How do I add a decimal point between the first 9 digits and the last 2 digits in the value that is being pulled in at line: $matchesLine[$key] = substr($line,166,11);
? As, the fetched values do not contain a decimal point.
Sample Data
Input: 00023659819
Desired output: #########.## >> 00236598.19
Script provided by @Shlomtzion
<?php
$text = "I0023540987805R01 ABC GHI OLirrt 000000000000000100EA 0812160070451700 1098833 1990041300000001086000000000108600000000000996000000000032100000000000000000000000000000000000000000000000000000000000000000000006589000000000000000 P0012B
0000002032902R01 DEF JKL KLijuI 000000000000000100EA 0812160070451700 1029132 1997010800000002396000000000239600000120002326000000000000000000000000000000000000000000000000000000000000004560000000000000000000000000987600000000 A203SD ";
echo '<pre>';
$txt = explode("\n",$text);
echo '<pre>';
print_r($txt);
foreach($txt as $key => $line){
$subbedString = substr($line,2,11);
$searchfor = '02354098780';
//echo strpos($subbedString,$searchfor);
if(strpos($subbedString,$searchfor) === 0){
$matches[$key] = $searchfor;
$matchesLine[$key] = substr($line,166,11);
echo "Found in line : $key";
}
}
echo '<pre>';
print_r($matches);
echo '<pre>';
print_r($matchesLine);
CodePudding user response:
Here is one way to this via preg_replace
:
$input = "00023659819";
$output = preg_replace("/(?=\d{2}$)/", ".", $input);
echo $output; // 000236598.19
CodePudding user response:
If you already know that there are 11 digits, you can use substr_replace
$str = "00023659819";
echo substr_replace($str, ".", 9, 0);
Output
000236598.19
If you want to do the replacement when 11 digits, you can use preg_replace with 2 capture groups for 9 and 2 digits and in the replacement use the 2 capture groups with a dot in between $1.$2
$str = "00023659819";
echo preg_replace("/^(\d{9})(\d{2})$/", "$1.$2", $str);
Output
000236598.19