php 修剪不起作用

发布于 2024-11-15 16:28:01 字数 844 浏览 3 评论 0原文

所以我有一个服务器端ajax php文件:

$thing = "\n\ncode\n";
echo(trim($thing, '\n'));

在此处输入图像描述

但是当我使用这个php文件进行ajax调用时,响应文本没有删除换行符!

在此处输入图像描述

var xhr7 = new XMLHttpRequest();
xhr7.onreadystatechange = function() {
  if (xhr7.readyState == 4) {
    if (xhr7.status == 200) {
      alert('trying');
      alert(xhr7.responseText);
      chrome.tabs.executeScript(null, {code: xhr7.responseText });
    } else {
      alert("404 server side ajax file DOES NOT EXIST");
    }
  }
};
xhr7.open('POST', 'http://texthmu.com/Devin/HMU%20ext/Post-jsfile-forItsCode.php', true);
xhr7.setRequestHeader("Content-type","application/x-www-form-urlencoded"); //following w3 
xhr7.send(); `

So I have a server side ajax php file:

$thing = "\n\ncode\n";
echo(trim($thing, '\n'));

enter image description here

But when I use this php file for an ajax call, the responseText does not have newlines removed!

enter image description here

var xhr7 = new XMLHttpRequest();
xhr7.onreadystatechange = function() {
  if (xhr7.readyState == 4) {
    if (xhr7.status == 200) {
      alert('trying');
      alert(xhr7.responseText);
      chrome.tabs.executeScript(null, {code: xhr7.responseText });
    } else {
      alert("404 server side ajax file DOES NOT EXIST");
    }
  }
};
xhr7.open('POST', 'http://texthmu.com/Devin/HMU%20ext/Post-jsfile-forItsCode.php', true);
xhr7.setRequestHeader("Content-type","application/x-www-form-urlencoded"); //following w3 
xhr7.send(); `

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

也只是曾经 2024-11-22 16:28:01

\n 需要用双引号而不是单引号

echo(trim($thing, "\n"));

当值用单引号括起来时,它会被解析为文字。在这种情况下, \n 而不是您要修剪的新行字符。

\n needs to be in double quotes not single

echo(trim($thing, "\n"));

When a value is in single quotes, it's parsed as a literal. In this case \n instead of the new line character you're trying to trim.

一影成城 2024-11-22 16:28:01

您需要将 \n 字符放在双引号 (") 字符串中。PHP

不会在单引号 (') 中解释这些特殊字符>) 分隔字符串。

You need to place the \n character in a double quoted (") string.

PHP doesn't interpet these special characters in single quote (') delimited strings.

雨后咖啡店 2024-11-22 16:28:01

只需使用

$thing = "\n\ncode\n"; 
echo(trim($thing));

参考: http://php.net/manual/en/function.trim.php

此函数返回一个字符串,该字符串从 str 的开头和结尾删除了空格。如果没有第二个参数,trim() 将删除这些字符:

  • " " (ASCII 32 (0x20)),普通空格。
  • “\t”(ASCII 9 (0x09)),制表符。
  • “\n”(ASCII 10 (0x0A)),新行(换行)。
  • “\r”(ASCII 13 (0x0D)),回车符。
  • “\0”(ASCII 0 (0x00)),NUL 字节。
  • “\x0B”(ASCII 11 (0x0B)),垂直制表符。

以下评论的补充

请注意;

$thing = "\n\nco\nde\n"; // See the \n between co and de ? 
echo(trim($thing, "\n"));

如果您也希望将其删除,那么修剪功能不适合您。

如果你想从字符串中删除所有\n,那么你应该使用

str_replace("\n", "", $thing);

Just use

$thing = "\n\ncode\n"; 
echo(trim($thing));

Ref: http://php.net/manual/en/function.trim.php

This function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter, trim() will strip these characters:

  • " " (ASCII 32 (0x20)), an ordinary space.
  • "\t" (ASCII 9 (0x09)), a tab.
  • "\n" (ASCII 10 (0x0A)), a new line (line feed).
  • "\r" (ASCII 13 (0x0D)), a carriage return.
  • "\0" (ASCII 0 (0x00)), the NUL-byte.
  • "\x0B" (ASCII 11 (0x0B)), a vertical tab.

Additional for the comments below

Please note that;

$thing = "\n\nco\nde\n"; // See the \n between co and de ? 
echo(trim($thing, "\n"));

If you wish that it be removed too, then trim is not the right function for you.

If you wish to remove ALL \n from a string, then you should use

str_replace("\n", "", $thing);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文