【问题标题】:Insert a chain in a file at a specific point在文件中的特定点插入链
【发布时间】:2013-02-18 13:24:26
【问题描述】:

我想在 php.ini 文件中的特定点插入一个链。

这是一个例子(我尝试过):

$insert = "Example of Chain";
$fileContent = file_get_contents($file);
//Example of content: codecodecode <tag> EXAMPLE EXAMPLE </tag> code code code
//Result i need  = codecodecode Example of Chain codecodecode

如您所见,我需要删除标签的内容及其 nodeValue 并将其替换为 $insert。另外,请注意我需要使用 file_get_contents。

感谢您的建议和技巧

这是一个尝试。此代码发现文件内容中的标签(通过 file_get_contents),并用从函数 retrieveLinks 返回的文本替换代码,但我不知道如何将文件中的文本放回正确的位置:

//如果它在''之间它不会出现所以我把它自愿这样说..不是一个错误

$start = '标签'; $end = '/标签';

 $startChain = strpos($fileContent, $start);
 $endChain = strpos($fileContent, $end) + strlen($end);

 $text = substr($fileContent, $startChain, $endChain - $startChain);

 require_once("showISComment.php");
 $newText = retrieveLinks($project);

 $content = str_replace($text, $newText, file_get_contents($file));


 //file_put_contents($file, $content);

【问题讨论】:

  • 但是你什么都没尝试。要替换文本中的某些内容,您始终可以使用 str_replace()
  • 我已经这样做了.. 将代码放在问题中。
  • 如果你知道标签,那么使用 str_replace 函数怎么样。

标签: php string file replace


【解决方案1】:

如果你知道标签的内容,那么就可以了

file_put_contents(
    $file,
    str_replace(
        '<tag> EXAMPLE EXAMPLE </tag>',
        $insert,
        file_get_contents($file)
    )
);

【讨论】:

  • 它会在文件中被替换并保存?
  • 是的,它会 file_put_contents 保存文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-18
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多