求段php 替换输出的代码
黄舟
黄舟 2017-04-10 15:28:24
0
2
298

循环那部分一直写不对

要求如下
读取a.txt 内容是
111,2222
333,4444
555,7777
这样的格式

如果$goods_id的值 等于其中一行前面的值 那么转换成后面的输出,比如 $goods_id=111,那么进行替换后输出$goods_id=2222

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(2)
洪涛
$goods_id = 111;

$handle = fopen("f:/test.txt", "r");
while (!feof($handle)) {
    $line = fgets($handle, 4096);
    //echo $line."<br>";
    $str = explode(",",$line);

    if($str[0] == $goods_id ){
        echo $str[1];
    }
}
    fclose($handle);
Ty80

`

function getGoodsIpMap()
{
    $fh = fopen('a.txt', 'r');
    $goodsMap = array();
    while($line=fgets($fh)!==false)
    {
        if(empty($line))
        {
            continue;
        }
        list($oldId, $newId) = explode(',',trim($line));
        $goodsMap[$oldId] = $newId;
    }
    fclose($fh);
}

$goodsIdMap = getGoodsIpMap();
$newId = isset($goodsIdMap[$goodsId]) ? $goodsIdMap[$goodsId] : $goodsId;

`

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template