PHP If语句带有特殊字符

时间:2014-11-12 11:59:56

标签: php file-io

我如何创建一个If语句或函数,如果找到特殊字符'}',那么在下一个字符中会放一个<br>

$myfile = fopen("result.txt", "r") or die("Unable to open file!");

while(!feof($myfile)) {
    echo fgetc($myfile);
}
fclose($myfile);

1 个答案:

答案 0 :(得分:3)

您只需阅读文件内容然后进行更换即可。

$contents = file_get_contents('file.txt');
$contents = str_replace('}', '}<br>', $contents);
echo $contents;

如果您只想在结束花括号后面换行,那么只需将该行更改为:

$contents = str_replace('}', "}\n", $contents);

如果您想再次保存,那么您可以这样做:

if ( ! file_put_contents('file.txt', $contents)) {
    echo 'Error. File could not be saved!';
}