php 在php中用逗号替换空格和换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17838096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:33:12  来源:igfitidea点击:

Replace spaces and newlines with a comma in php

php

提问by salty

I searched but couldn't find anything that helped

我搜索但找不到任何帮助

So, I have a bunch of data I need to basically just put commas between, the data all comes from records so it comes in like:

所以,我有一堆数据,我基本上只需要在它们之间加上逗号,这些数据都来自记录,所以它的输入方式如下:

13
566
888
958
898

and I need to output it like:

我需要像这样输出它:

13,566,888,958,898

even better if I can move any 2 digits one over like:

如果我可以将任何两位数字移动一个,那就更好了:

566,13,888,958,989

I know it should be pretty simple, so basically I just made a form, pass it with GET and then read the variable from the url, then I'm trying to use str_replace(" ",",",$outtext) to get replace the spaces with commas so I can echo it out and just copypasta it in real quick, problem is, I think the data comes in with a new line and I don't know how to replace that, I've tried "\n" and "%0D%0A" but it doesn't replace it, it just gives it right back to me with the spaces.

我知道它应该很简单,所以基本上我只是做了一个表单,用 GET 传递它,然后从 url 读取变量,然后我尝试使用 str_replace(" ",",",$outtext) 来获取用逗号替换空格,这样我就可以将其回显出来,然后快速复制它,问题是,我认为数据带有一个新行,但我不知道如何替换它,我试过“\n " 和 "%0D%0A" 但它没有替换它,它只是把它和空格一起还给我。

Anyway, here's the code I'm using, maybe someone can tell me the simple thing that I'm missing,

无论如何,这是我正在使用的代码,也许有人可以告诉我我错过的简单事情,

<?php
if (isset($_GET['intext'])) {
    $outtext=$_GET['intext'];

    $newout=str_replace(" ", ",", $outtext);

    echo $newout;
    //testing
    //echo(str_replace(" ",",","$outtext"));
}
?>

and the html

和 html

<form name="indata" id="indata" method="get" action="index.php">
        <textarea id="intext" name="intext"></textarea>
        <input type="submit" id="submit" name="submit">
    </form>

I also thought about tossing it into an array and imploding it, but it's been a while and I forget how to do that.

我也想过把它扔进一个数组并内爆它,但已经有一段时间了,我忘记了怎么做。

回答by SmokeyPHP

The newline could still be registering if you've got the "\r\n" version of a newline - you're removing "\n" and leaving "\r" behind.

如果您有换行符的 "\r\n" 版本,换行符仍然可以注册 - 您正在删除 "\n" 并留下 "\r" 。

$str = preg_replace('/\s+/',',',str_replace(array("\r\n","\r","\n"),' ',trim($str)));

Cleaner (more legible) version:

更干净(更清晰)的版本:

$str = preg_replace('#\s+#',',',trim($str));

回答by Jason McCreary

As you said, using an array then imploding is a good approach:

正如您所说,使用数组然后内爆是一个好方法:

$input = trim($_GET['intext']);
$numbers = preg_split('/\s+/', $input);
echo implode(',', $numbers);

This trims the input of any padding, splits into an array based on whitespace, then implodes the array with commas.

这会修剪任何填充的输入,根据空格拆分为一个数组,然后使用逗号对数组进行内爆。

回答by Konsole

First remove any extra white spaces or carriage returns and new lines from your input then replace it with comma.

首先从您的输入中删除任何额外的空格或回车符和新行,然后用逗号替换它。

 <?php
  if( isset($_GET['intext']) ){
      var_dump($_GET['intext']);

      //get and trim the input
      $value = trim($_GET['intext']);

      //clean the input by removing extra white spaces
      $value = preg_replace( '/\s+/', ' ', $value );

      //replace spaces with comma
      $value = str_replace(" ", ',', $value);

      //echo output
      echo $value;
  }
 ?>

 <form name="indata" id="indata" method="get" action="">
    <textarea id="intext" name="intext"></textarea>
    <input type="submit" id="submit" name="submit">
 </form>

回答by txpeaceofficer09

In *nix (and I believe mac as well) systems you have lines separated by LF "\n" and on windows you have CR+LF "\r\n". You could do a str_replaceor a preg_replaceand replace the spaces, the CR+LF and then the LF. You could use a regex to replace anything that resolve in your regex with a comma.

在 *nix(我相信 mac 也是如此)系统中,您的行由 LF "\n" 分隔,而在 Windows 上,您有 CR+LF "\r\n"。您可以执行 astr_replace或 apreg_replace并替换空格、CR+LF 然后是 LF。您可以使用正则表达式用逗号替换正则表达式中解析的任何内容。

Show the code that is not replacing line feeds and the source of the string and perhaps I can tell you why it isn't working.

显示不替换换行符的代码和字符串的来源,也许我可以告诉你为什么它不起作用。