Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 碰到一个问题,我觉得肯定有简便写法,但是一时找不到。

碰到一个问题,我觉得肯定有简便写法,但是一时找不到。

Jun 23, 2016 pm 02:11 PM

config.php

array(
  'title='>"123",
  'title2'=>"123",
  'title3'=>"123",
  ......
  无限个
)

这时我一个操作只想改变title2的值,比如改成456
难道一定要全部读取,再重新一次写入文件中?


回复讨论(解决方案)

$array['title2'] = 456;

哦,错了,原来是写进文件。

有两个方案
1,如果文件比较大,就fgets,一行一行读吧,直到读到的为title2,但这个有个坏处,就是注释问题。

2,file_get_content 再 file_put_content

对!需要重新写入
所以大量的数据应用数据库

文件巨大的话可以在#2的方案变通一下,每读一行,视乎需要改动,写入新文件
全部完成后删掉旧文件(或改名bak),再把新文件改名为原来的文件

前提是权限足够

为什么要边读边写呢?
楼主的是 config.php 文件,显然是已经读取进来了的
所以只需将修改后的数组用 var_export 构造成 php 代码写回去就可以了

我说的是文件巨大的情况,如果是config估计也不怎么巨大,

为什么要边读边写呢?
楼主的是 config.php 文件,显然是已经读取进来了的
所以只需将修改后的数组用 var_export 构造成 php 代码写回去就可以了

<?php$handle = fopen("config.php", "r+");$offset = 0;if ($handle) {    while (!feof($handle)) {        $buffer = fgets($handle); //默认为1024         $offset += strlen($buffer);         if(false !== strpos($buffer, '需要替换的字符串')){              fwrite($handle, '你的内容//');把后面的内容注释掉^.^              break;         }    }    fclose($handle);}?> 
Copy after login


指针可能不正确,你需要调试一下!

换个思路吧,

当遇到这种问题的时候,就该想想是不是方向错了。

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles