Php 为什么它要在下一行的开头添加文本,而不是右行的结尾?
需要的解决方案:Php 为什么它要在下一行的开头添加文本,而不是右行的结尾?,php,regex,flat-file,Php,Regex,Flat File,需要的解决方案: <?php // specify the file $file_source="newstest.db"; // get the content of the file $newscontent = file($file_source, true); //set a start value (clear memory) $lines =''; // get each line and treat it line by line. foreach ($newsco
<?php
// specify the file
$file_source="newstest.db";
// get the content of the file
$newscontent = file($file_source, true);
//set a start value (clear memory)
$lines ='';
// get each line and treat it line by line.
foreach ($newscontent as $line_num => $linehere) {
// add "value4:::" at the end of FIRST line only, and put it in memory $lines
if($line_num==0) {
$lines.= $linehere.'value4:::';
// Just to see what the line looks like
//echo 'First line: '.$lines.'<br /><br />';
}
// then add ":::" to the other lines and add them to memory $lines
if($line_num>0) {
$lines1 = $linehere.':::';
$lines.= $lines1;
//just look at the line
//echo 'Line #'.$line_num.': '.$lines1.'<br /><br />';
}
}
//Write new content to $file_source
$f = fopen($file_source, 'w');
fwrite($f,$lines);
fclose($f);
echo "// to show the results ar array<br /><br />";
$newscontentlook = file($file_source, true);
print_r(array_values($newscontentlook));
?>
我正在使用一个简单的PHP脚本,它应该:
信息:
<?php
// specify the file
$file_source="newstest.db";
// get the content of the file
$newscontent = file($file_source, true);
//set a start value (clear memory)
$lines ='';
// get each line and treat it line by line.
foreach ($newscontent as $line_num => $linehere) {
// add "value4:::" at the end of FIRST line only, and put it in memory $lines
if($line_num==0) {
$lines.= $linehere.'value4:::';
// Just to see what the line looks like
//echo 'First line: '.$lines.'<br /><br />';
}
// then add ":::" to the other lines and add them to memory $lines
if($line_num>0) {
$lines1 = $linehere.':::';
$lines.= $lines1;
//just look at the line
//echo 'Line #'.$line_num.': '.$lines1.'<br /><br />';
}
}
//Write new content to $file_source
$f = fopen($file_source, 'w');
fwrite($f,$lines);
fclose($f);
echo "// to show the results ar array<br /><br />";
$newscontentlook = file($file_source, true);
print_r(array_values($newscontentlook));
?>
文件newstest.db如下所示:
ID:::value1:::value2:::value3:::
1:::My:::first:::line:::
2:::My:::second:::line:::
3:::Your:::third:::line:::
ID:::value1:::value2:::value3:::value4:::
1:::My:::first:::line::::::
2:::My:::second:::line::::::
3:::Your:::third:::line::::::
ID:::value1:::value2:::value3:::
value4:::1:::My:::first:::line:::
:::2:::My:::second:::line:::
:::3:::Your:::third:::line:::
使用这个php脚本,我想让它看起来像这样:
ID:::value1:::value2:::value3:::
1:::My:::first:::line:::
2:::My:::second:::line:::
3:::Your:::third:::line:::
ID:::value1:::value2:::value3:::value4:::
1:::My:::first:::line::::::
2:::My:::second:::line::::::
3:::Your:::third:::line::::::
ID:::value1:::value2:::value3:::
value4:::1:::My:::first:::line:::
:::2:::My:::second:::line:::
:::3:::Your:::third:::line:::
问题:
<?php
// specify the file
$file_source="newstest.db";
// get the content of the file
$newscontent = file($file_source, true);
//set a start value (clear memory)
$lines ='';
// get each line and treat it line by line.
foreach ($newscontent as $line_num => $linehere) {
// add "value4:::" at the end of FIRST line only, and put it in memory $lines
if($line_num==0) {
$lines.= $linehere.'value4:::';
// Just to see what the line looks like
//echo 'First line: '.$lines.'<br /><br />';
}
// then add ":::" to the other lines and add them to memory $lines
if($line_num>0) {
$lines1 = $linehere.':::';
$lines.= $lines1;
//just look at the line
//echo 'Line #'.$line_num.': '.$lines1.'<br /><br />';
}
}
//Write new content to $file_source
$f = fopen($file_source, 'w');
fwrite($f,$lines);
fclose($f);
echo "// to show the results ar array<br /><br />";
$newscontentlook = file($file_source, true);
print_r(array_values($newscontentlook));
?>
到目前为止,我几乎已经了解了它,但我不明白为什么它会在第二行的开头添加“value4:::”,然后在所有其他行的开头(而不是结尾)添加“:::”
所以我得到了一个如下的文件:
ID:::value1:::value2:::value3:::
1:::My:::first:::line:::
2:::My:::second:::line:::
3:::Your:::third:::line:::
ID:::value1:::value2:::value3:::value4:::
1:::My:::first:::line::::::
2:::My:::second:::line::::::
3:::Your:::third:::line::::::
ID:::value1:::value2:::value3:::
value4:::1:::My:::first:::line:::
:::2:::My:::second:::line:::
:::3:::Your:::third:::line:::
我想:
$lineshere = 'Some text here:::';
$lines1 = $linehere.'value4:::';
将输出“此处的一些文本:::值4::”
问题可能是由于这种逐行添加的方式造成的
$lines = '';
$lines.= 'My test';
$lines.= ' is here';
echo $lines;
我是一个编程新手,所以我可能使用了完全错误的函数来实现这一点
但在这种情况下,需要在错误的位置添加空格或换行符/换行符
我尝试此解决方案:
<?php
// specify the file
$file_source="newstest.db";
// get the content of the file
$newscontent = file($file_source, true);
//set a start value (clear memory)
$lines ='';
// get each line and treat it line by line.
foreach ($newscontent as $line_num => $linehere) {
// add "value4:::" at the end of FIRST line only, and put it in memory $lines
if($line_num==0) {
$lines.= $linehere.'value4:::';
// Just to see what the line looks like
//echo 'First line: '.$lines.'<br /><br />';
}
// then add ":::" to the other lines and add them to memory $lines
if($line_num>0) {
$lines1 = $linehere.':::';
$lines.= $lines1;
//just look at the line
//echo 'Line #'.$line_num.': '.$lines1.'<br /><br />';
}
}
//Write new content to $file_source
$f = fopen($file_source, 'w');
fwrite($f,$lines);
fclose($f);
echo "// to show the results ar array<br /><br />";
$newscontentlook = file($file_source, true);
print_r(array_values($newscontentlook));
?>
函数file()
以数组的形式提供文件,每行都是数组的一个元素。行的每个结尾(EOL
)都包含在元素中。因此,将文本追加到该行将位于下线之后,因此有效地位于下一行的开头
您可以选择不使用file()
并自己在EOL
上分解文本,这样EOL
就不再是数组元素的一部分。然后编辑元素,并再次内爆阵列
或者
fopen()
文件,然后自己通过它fread()
。后一个选项是首选选项,因为它不会立即将整个文件加载到内存中。我认为问题出在通过文件()函数读取的行的循环中:
foreach ($newscontent as $line_num => $linehere) {
...
$linehere
末尾包含一个换行符,因此您只需在使用前将其切碎:
foreach ($newscontent as $line_num => $linehere) {
$linehere = chop($linehere);
...
如果不chop
行内容,将字符串连接到行内容时,将得到:
LINE_CONTENTS\nSTRING_ADDED
打印时,将:
LINE_CONTENTS
STRING_ADDED
希望这有助于……这实际上很容易通过和实现,即:
输出:
ID:::value1:::value2:::value3:::value4:::
1:::My:::first:::line::::::
2:::My:::second:::line::::::
3:::Your:::third:::line::::::
作为旁注:您可能希望使用十六进制编辑器查看文件,以查看换行符/回车符。我相信,这是因为$linehere以换行符结尾。行
$newcontent=file($file\u source,true)中的true
的目的是什么代码>?将其更改为FILE\u IGNORE\u NEW\u LINES
,它应该可以工作。使用FILE\u get\u contents
和preg\u replace
很容易实现这一点,我在下面发布了一个答案,非常感谢大家。我正在查看所有建议的解决方案:-)很好的解释。但是,通过添加标志FILE\u IGNORE\u NEW\u LINES
(如果我没有完全错的话),可以用当前的方法解决这个问题。这是一个聪明的方法。不过我有一个问题。现在,它在第一行下面添加了“value4:::”作为新行,等等。每一个新行都是这样的::”。知道为什么吗?它与我获取文件内容的方式有关吗?尝试在两个正则表达式的末尾添加\S
,我已经更新了答案\S
匹配不是“空白字符”的单个字符(任何Unicode分隔符、制表符、换行符、回车符、垂直制表符、换行符、下一行)«\S»对不起。说到正则表达式,我真的是个笨蛋。我不知道该把\S
放在哪里。我在im
之后试过了,但没有成功。应该是什么样子?看看我的答案,就在那里哦。我看了上面的答案。我现在在deone.com的链接中看到了它,这解决了空间问题:-)但是现在当写回文件时,所有内容都保存在一行中。所以我可能需要添加一个代码,告诉它用“行尾”保存每一行?你知道怎么写吗?是的:你可以在写每一行时使用fputs()
而不是fwrite()
,或者在fwrite()
之前的每一行末尾添加一个\n
字符。。。