读取外部文件匹配第一列中的特定字符串和 return 中第二列的相应字符串 php
Read external file match specific string in first column and return respective string of second column in php
我有两个文本文件,csvurl.txt 和 tickerMaster.txt
tickerMaster.txt
H0001
备注:tickerMaster.txt中没有"H0003"且编号不按顺序
csvurl.txt
H0001, URL1
H0003, URL3
我想一一阅读tickerMaster.txt中的条目,例如H0001、H0003...
并通过匹配 csvurl.txt 中的数据来创建 URL。所以我正在使用以下代码...
<?php
function createURL($ticker){
$file = 'csvurl.txt';
header('Content-Type: text/plain');
$contents = file_get_contents($file);
$sep = ',';
$pattern = preg_quote($searchfor, '/');
$searchfor = $ticker;
$pattern = "/^($searchfor\w+)$sep.*$/m";
if (preg_match_all($pattern, $contents, $matches)){
echo implode($matches[0])."\n";
}
else{
echo "No matches found";
}
}
function getCSVFile($url, $outputFile){
$content = file_get_contents($url);
$content = str_replace("Date,Open,High,Low,Close,Volume,Adj Close", "", $content);
$content = trim($content);
file_put_contents($outputFile, $content);
}
function fileToDatabase($txtFile, $tableName){
$file = fopen($txtFile, "r");
while(!feof($file)){
$line = fgets($file);
$pieces = explode(",", $line);
$date = $pieces[0];
$open = $pieces[1];
$high = $pieces[2];
$low = $pieces[3];
$close = $pieces[4];
$volume = $pieces[5];
$amount_change = $close-$open;
$percent_change = ($amount_change/$open)*100;
$sql = "SELECT * FROM $tableName";
$result = mysql_query($sql);
if(!$result){
$sql2 = "CREATE TABLE $tableName (date DATE, PRIMARY KEY(date), open FLOAT, high FLOAT, low FLOAT, close FLOAT, volume INT, amount_change FLOAT, percent_change FLOAT)";
mysql_query($sql2);
}
$sql3 = "INSERT INTO $tableName (date, open, high, low, close, volume, amount_change, percent_change) VALUES ('$date','$open','$high','$low','$close','$volume', '$amount_change', '$percent_change')";
mysql_query($sql3);
}
fclose($file);
}
function main(){
$mainTickerFile = fopen("tickerMaster.txt", "r");
while(!feof($mainTickerFile)){
$companyTicker = fgets($mainTickerFile);
$companyTicker = trim($companyTicker);
$fileURL = createURL($companyTicker);
$companyTxtFile = "txtFiles/".$companyTicker.".txt";
getCSVFile($fileURL, $companyTxtFile);
fileToDatabase($companyTxtFile, $companyTicker);
}
}
main()
?>
但是,我得到的是csvurl.txt中的整行信息
例如:
找不到匹配项 H0001,URL1H0003,URL3
我想要的输出是:
网址 1
实际上,我正在excel中寻找类似vlookup的功能,但我无法搜索到任何解决方案来进行这种匹配。
谢谢
我想数据没有错误,所以不要做任何测试
$c1 = file('csvurl.txt');
$l = count($c1);
for ($i = 0; $i < $l; $i++) {
list($name,$url) = explode(',', $c1[$i]);
// making array $red['H001'] => 'URL1"
$red[trim($name)] = trim($url);
}
unset($c1);
$c = file('tickerMaster.txt');
$l = count($c);
for ($i = 0; $i < $l; $i++) {
$c[$i] = trim($c[$i]);
// If rule exists
if(isset($red[$c[$i]])) echo($red[$c[$i]]);
}
我有两个文本文件,csvurl.txt 和 tickerMaster.txt
tickerMaster.txt
H0001
备注:tickerMaster.txt中没有"H0003"且编号不按顺序
csvurl.txt
H0001, URL1
H0003, URL3
我想一一阅读tickerMaster.txt中的条目,例如H0001、H0003...
并通过匹配 csvurl.txt 中的数据来创建 URL。所以我正在使用以下代码...
<?php
function createURL($ticker){
$file = 'csvurl.txt';
header('Content-Type: text/plain');
$contents = file_get_contents($file);
$sep = ',';
$pattern = preg_quote($searchfor, '/');
$searchfor = $ticker;
$pattern = "/^($searchfor\w+)$sep.*$/m";
if (preg_match_all($pattern, $contents, $matches)){
echo implode($matches[0])."\n";
}
else{
echo "No matches found";
}
}
function getCSVFile($url, $outputFile){
$content = file_get_contents($url);
$content = str_replace("Date,Open,High,Low,Close,Volume,Adj Close", "", $content);
$content = trim($content);
file_put_contents($outputFile, $content);
}
function fileToDatabase($txtFile, $tableName){
$file = fopen($txtFile, "r");
while(!feof($file)){
$line = fgets($file);
$pieces = explode(",", $line);
$date = $pieces[0];
$open = $pieces[1];
$high = $pieces[2];
$low = $pieces[3];
$close = $pieces[4];
$volume = $pieces[5];
$amount_change = $close-$open;
$percent_change = ($amount_change/$open)*100;
$sql = "SELECT * FROM $tableName";
$result = mysql_query($sql);
if(!$result){
$sql2 = "CREATE TABLE $tableName (date DATE, PRIMARY KEY(date), open FLOAT, high FLOAT, low FLOAT, close FLOAT, volume INT, amount_change FLOAT, percent_change FLOAT)";
mysql_query($sql2);
}
$sql3 = "INSERT INTO $tableName (date, open, high, low, close, volume, amount_change, percent_change) VALUES ('$date','$open','$high','$low','$close','$volume', '$amount_change', '$percent_change')";
mysql_query($sql3);
}
fclose($file);
}
function main(){
$mainTickerFile = fopen("tickerMaster.txt", "r");
while(!feof($mainTickerFile)){
$companyTicker = fgets($mainTickerFile);
$companyTicker = trim($companyTicker);
$fileURL = createURL($companyTicker);
$companyTxtFile = "txtFiles/".$companyTicker.".txt";
getCSVFile($fileURL, $companyTxtFile);
fileToDatabase($companyTxtFile, $companyTicker);
}
}
main()
?>
但是,我得到的是csvurl.txt中的整行信息
例如:
找不到匹配项 H0001,URL1H0003,URL3
我想要的输出是:
网址 1
实际上,我正在excel中寻找类似vlookup的功能,但我无法搜索到任何解决方案来进行这种匹配。 谢谢
我想数据没有错误,所以不要做任何测试
$c1 = file('csvurl.txt');
$l = count($c1);
for ($i = 0; $i < $l; $i++) {
list($name,$url) = explode(',', $c1[$i]);
// making array $red['H001'] => 'URL1"
$red[trim($name)] = trim($url);
}
unset($c1);
$c = file('tickerMaster.txt');
$l = count($c);
for ($i = 0; $i < $l; $i++) {
$c[$i] = trim($c[$i]);
// If rule exists
if(isset($red[$c[$i]])) echo($red[$c[$i]]);
}