Home > Backend Development > PHP Tutorial > 求段php 替换输出的代码

求段php 替换输出的代码

WBOY
Release: 2016-06-06 20:36:37
Original
1204 people have browsed it

循环那部分一直写不对

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

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

回复内容:

循环那部分一直写不对

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

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

<code>$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);
</code>
Copy after login

`

<code>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;
</code>
Copy after login

`

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template