[英]Removing/Adding characters from the end of a Text file using PHP
I have an autofill search that reads the contents of a text file (this text file has to be formatted in the correct way or the search doesn't work). 我有一个自动填充搜索,可以读取文本文件的内容(此文本文件必须以正确的方式设置格式,否则搜索将不起作用)。 Now Im doing it in a rather long winded way because I need square brackets at the start and end of the file. 现在,我以一种相当漫长的方式进行操作,因为我需要在文件的开头和结尾加上方括号。
Php script that gets the data from database: 从数据库获取数据的PHP脚本:
$getData = mysql_query("SELECT Name FROM tblIngredient");
while($info = mysql_fetch_array($getData))
{
$string = '{"key": "'.$info['Name'].'", "value": "'.$info['Name'].'"}, ';
$fp = fopen('data_old.txt', 'a');
fwrite($fp, $string);
fclose($fp);
}
second php script that opens data_old.txt and writes it with "[" at the start and "]" at the end, then saves it as data.txt: 第二个php脚本,它打开data_old.txt并在其开头写上“ [”,在末尾写上“]”,然后将其另存为data.txt:
$string1 = '[' . file_get_contents('data_old.txt') . ']';
$fp = fopen('data.txt', 'a');
fwrite($fp, $string1);
fclose($fp);
This saves the data into this format: (start of file) 这会将数据保存为以下格式:(文件开头)
[{"key": "vodka", "value": "vodka"}, {"key": "tequila", "value": "tequila"}, {
(end of file) (文件末尾)
{"key": "brandy", "value": "brandy"}, {"key": "beer", "value": "beer"}, ]
My problem is that at the end of the file there is a comma and space : }, ] and I need it to look like this: }] 我的问题是文件的末尾有一个逗号和空格:},],我需要它看起来像这样:}]
I have been googling and tried this to no avail 我一直在谷歌搜索,并尝试这样做无济于事
(I need code to truncate the last 2 characters of the data.txt file and write it again) (我需要代码来截断data.txt文件的最后2个字符并再次写入)
Any help is appreciated 任何帮助表示赞赏
-Matt -马特
edit2:: edit2 ::
Script1: 脚本1:
$getData = mysql_query("SELECT Name FROM tblIndredient");
while($info = mysql_fetch_array($getData))
{
$string = ', {"key": "'.$info['Name'].'", "value": "'.$info['Name'].'"}';
$fp = fopen('data.txt', 'a');
fwrite($fp, $string);
fclose($fp);
}
$content = file_get_contents('data.txt');
$content = '[' . ltrim('[, ', $content);
file_put_contents('data.txt');
Script2: 脚本2:
$string1 = '['. file_get_contents('data.txt') .']';
$fp = fopen('data.txt', 'a');
fwrite($fp, $string1);
fclose($fp);
$string1 = '[' . file_get_contents('data_old.txt');
$fp = fopen('data.txt', 'a');
fwrite($fp, $string1);
fclose($fp);
$files = fopen('data.txt', 'w+');
$content = fread($files, filesize('data.txt'));
$content = substr_replace($content ,"", -2);
$content .= ']';
fwrite($files, $content);
fclose($files);
change this line 改变这条线
$string = '{"key": "'.$info['Name'].'", "value": "'.$info['Name'].'"}, '; $ string ='{“ key”:“'。$ info ['Name']。'”,“ value”:“'。$ info ['Name']。'”},';
to 至
$string = ', {"key": "'.$info['Name'].'", "value": "'.$info['Name'].'"}'; $ string =',{“ key”:“'。$ info ['Name']。'”,“ value”:“'。$ info ['Name']。'”}';;
and change this 并改变这个
$string1 = '['. $ string1 ='['。 file_get_contents('data.txt') .']'; file_get_contents('data.txt')。']';
to 至
$string1 = file_get_contents('data.txt');
$string1 = '[' . ltrim($string1, ', ') . ']';
does that help ? 这有帮助吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.