读取第一列中的外部文件匹配特定字符串,并返回php

时间:2015-05-08 13:39:53

标签: php string return matching vlookup

我有两个文本文件,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

我想要的输出只是:
URL1

实际上,我在excel中寻找像vlookup这样的函数,但是我无法搜索任何这种匹配的解决方案。 感谢。

1 个答案:

答案 0 :(得分:1)

我支持数据没有错误,所以不要做任何测试

    $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]]);
    }