在PHP中读取文件txt中的一行

时间:2015-06-25 08:01:27

标签: php file line readfile requirements.txt

我想在文件文本中读取一行。 EX line 5.任何人都可以帮助我????

class bom_override(models.Model):
_name = 'mrp.bom'
_inherit = 'mrp.bom'

@api.model
def create(self, cr, uid, vals, context=None):
    product_obj = self.env['product.template'].sudo().search_read([('id','=',vals['product_id'])])
    product_obj.write({'has_bom':True})
    res = super(bom_override, self).create(cr, uid, vals, context=context)
    return res

感谢您的阅读

3 个答案:

答案 0 :(得分:3)

您可以使用fgets()函数逐行读取文件:

didFinishLaunching

答案 1 :(得分:0)

只需在循环中放置一个增量计数器,只有当该计数器与您需要的行号匹配时才会回显,然后您可以简单地摆脱循环

$required = 5;

$line = 1;
$fp=fopen("test.txt",r)or exit("khong tim thay file can mo");
while(!feof($fp)){
    if ($line == $required) {
        echo fgets($fp);
        break;
    }
    ++$line;
}
fclose($fp);

答案 2 :(得分:0)

$myFile = "text.txt";
$lines = file($myFile);//file in to an array
echo $lines[1]; //line 2

PHP - Reads entire file into an array