gpt4 book ai didi

php - 修剪不删除结束换行符?

转载 作者:可可西里 更新时间:2023-11-01 00:14:54 30 4
gpt4 key购买 nike

这应该相当简单。假设我有以下代码:

$output = file_get_contents($random_name . ".txt");
echo "<pre>";
echo str_replace("\n", "\nN ", $output);
echo "</pre>";

$output 看起来像这样:

PDF Test File
N Congratulations, your computer is equipped with a PDF (Portable Document Format)
N reader! You should be able to view any of the PDF documents and forms available on
N our site. PDF forms are indicated by these icons:
N or.
N
N

假设我想通过以下方法摆脱最后两个换行符:

$outputTrimmed = trim($output, "\n");

我假设,那会输出:

PDF Test File
N Congratulations, your computer is equipped with a PDF (Portable Document Format)
N reader! You should be able to view any of the PDF documents and forms available on
N our site. PDF forms are indicated by these icons:
N or.

而是这段代码:

$output = file_get_contents($random_name . ".txt");
$outputTrimmed = trim($output, "\n");
echo "<pre>";
echo str_replace("\n", "\nN ", $outputTrimmed);
echo "</pre>";

结果:

PDF Test File
N Congratulations, your computer is equipped with a PDF (Portable Document Format)
N reader! You should be able to view any of the PDF documents and forms available on
N our site. PDF forms are indicated by these icons:
N or.
N
N

我做错了什么?这可能是真的真的简单的……所以我道歉。

最佳答案

您可能正在使用 Windows 行尾样式。

这是\r\n,而不仅仅是\n

尝试更换两者。

或者,不指定任何字符列表(第二个参数)。通过指定 \n 你是说 ONLY trim \n

修剪($output)

在此处查看文档:http://www.w3schools.com/php/func_string_trim.asp#gsc.tab=0


编辑(根据您的评论):

如果 trim() 不起作用,请尝试将您的字符串更改为字节数组并检查准确您的字符串末尾的字符是什么。这让我怀疑还有其他一些不可打印的字符干扰。

$byteArray = unpack('C*', $output);
var_dump($byteArray);

http://www.asciitable.com/

关于php - 修剪不删除结束换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15779175/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com