I am fetching data from a text file. How can I fetch the value in between the two delimiters i.e. | based on the match. The first 7 characters are part of the id.
Input Example:
0123456|BHKAHHHHkk|12345678|JuiKKK121255
9100450|HHkk|12348888|JuiKKK10000000021sdadad255
Desired Output:
BHKAHHHHkk based on the searchfor value = 0123456
My Script:
$file = 'file.txt';
// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');
// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$txt = explode("\n",$contents);
$counter = 0;
foreach($txt as $key => $line){
$subbedString = substr($line,2,6);
// $searchfor = '0123456';
//echo strpos($subbedString,$searchfor);
if(strpos($subbedString,$searchfor) === 0){
$matches[$key] = $searchfor;
echo "<p>" . $matches[$key] . "</p>";
$counter += 1;
if($counter==10) break;
}