Php replace comma with newline

I do know that php can turn a newline into a
using nl2br(), but is there a way that it can turn a comma (,) into a
?

For instance on an HTML page a user enters a bunch of words that are separated by a comma. He then submits it, it gets sent to a PHP file like normal and instead of the data getting sent right into the database, it goes through some kind of function to replace the commas with
.

So if I were to show the data on an HTML page, each thing they entered in that was separated by a comma is now separated by a newline/line break?

CodeCaster

141k22 gold badges208 silver badges255 bronze badges

asked Feb 21, 2012 at 14:27

3

You can use a str_replace() to replace any string by another, so also a comma with a
:

$output = str_replace(',', '
', $input);

answered Feb 21, 2012 at 14:29

CodeCasterCodeCaster

141k22 gold badges208 silver badges255 bronze badges

0

$values = str_replace( ',', '
', $values );

answered Feb 21, 2012 at 14:29

MetalFrogMetalFrog

9,4831 gold badge21 silver badges23 bronze badges

$output = str_replace(',', "\n", $input);

Vasfed

17.5k10 gold badges48 silver badges52 bronze badges

answered Jan 13, 2015 at 10:01

Php replace comma with newline

SBoxSBox

511 silver badge5 bronze badges

I think php.net : str_replace is what you need.

answered Feb 21, 2012 at 14:30

Php replace comma with newline

AbbasAbbas

13.9k6 gold badges41 silver badges69 bronze badges

    Table of contents
  • Replace spaces and newlines with a comma in php
  • Replacing new line characters with comma from given text
  • Replacing Whitespaces and Newlines with commas Code Answer
  • Remove new lines from string and replace with one empty space
  • Replace Multiple Spaces and Newlines with only one space in PHP

Replace spaces and newlines with a comma in php

13
566
888
958
898
13,566,888,958,898
566,13,888,958,989
$str = preg_replace('/\s+/',',',str_replace(array("\r\n","\r","\n"),' ',trim($str)));
$str = preg_replace('#\s+#',',',trim($str));
$input = trim($_GET['intext']);
$numbers = preg_split('/\s+/', $input);
echo implode(',', $numbers);
 

 

Replacing new line characters with comma from given text

$letters = '#\s+#';
$rep   = ',';
$output  = str_replace($letters, $rep, trim($text));
echo $output;
$output = str_replace(array("\n", "\r\n"), ",", $text);
preg_replace('|\n|', ',', $text)

Replacing Whitespaces and Newlines with commas Code Answer

preg_replace('#s+#',',',trim($str));
$str = "
a
b
c
";
$str = "
a,
b,
c
";
$str = "
a ,
b ,
c ,
";
/[s,]+$n|[s,]+/gm
/[s,]+/

Remove new lines from string and replace with one empty space

$string = "
put returns between paragraphs

for linebreak add 2 spaces at end

";
/\r\n|\r|\n/
$string = "put returns between paragraphs for linebreak add 2 spaces at end ";
$string = trim(preg_replace('/\s\s+/', ' ', $string));
$string = trim(preg_replace('/\s+/', ' ', $string));
$string = preg_replace('/\s+/', ' ', trim($string));
$string = preg_replace('~[\r\n]+~', '', $string);
$string = str_replace(array("\n", "\r"), '', $string);
$string = str_replace(array("\n", "\r"), ' ', $string);
str_replace(array("\r\n","\r"),"",$string);
// Create an array with the values you want to replace
$searches = array("\r", "\n", "\r\n");

// Replace the line breaks with a space
$string = str_replace($searches, " ", $string);

// Replace multiple spaces with one
$output = preg_replace('!\s+!', ' ', $string);
$string = preg_replace('/\R+/', " ", $string);
$new_string = preg_replace("/\r\n|\r|\n/", ' ', $old_string);
$string = preg_replace("/[\\n\\r]+/", "", $string);
$string = preg_replace("/[\\n\\r]+/", " ", $string);
$string = trim( str_replace( PHP_EOL, ' ', $string ) );
//Removes all 3 types of line breaks

$string = str_replace("\r", "", $string);

$string = str_replace("\n", "", $string);
$str = preg_replace('/\r?\n$/', ' ', $str);

$str =~ s/\r?\n$/ /g;
$string = preg_replace('@[\s]{2,}@',' ',$string);
$svgxml = preg_replace("/(*BSR_ANYCRLF)\R/",'',$svgxml);
$string = preg_replace('~[\r\n\t]+~', '', $text);
$str = "
put returns between paragraphs

for linebreak add 2 spaces at end

";

echo preg_replace( "/\r|\n/", "", $str );
$str='\n';
echo str_replace('\n','',$str);
$pattern = '~[\r\n\s?]+~';
$name="test1 /
                     test1";
$name = preg_replace( $pattern, "$1 $2",$name);

echo $name;
$hello = "
A
B
C
";
str_replace("
", " ", $hello);
// A B C
$des = str_replace('\n',' ',$des);
$des = str_replace('\r',' ',$des);
 $string = trim(str_replace('\n', '', (str_replace('\r', '', $string))));

Replace Multiple Spaces and Newlines with only one space in PHP

This is         a dummy text.               I need              




to                                      format
this.
This is a dummy text. I need to format this.
$replacer  = array("\r\n", "\n", "\r", "\t", "  ");
$string = str_replace($replacer, "", $string);
# string(45) "This is a dummy text . I need to format this."
$str = preg_replace( "/\s+/", " ", $str );
$patterns = array("/\s+/", "/\s([?.!])/");
$replacer = array(" ","$1");

# string(44) "This is a dummy text. I need to format this."
$str = preg_replace( $patterns, $replacer, $str );

Next Lesson PHP Tutorial