anonamas
anonamas

Reputation: 241

Replace textarea linebreaks in php

I am trying to change the output from $foo, replace linebreaks with ";". Here is an explanation and my preg match, however it does not work. The output is the same

<?php
/* $foo
1554
6554
5543
*/

preg_replace('/^\s+|\n|\r|\s+$/m', ';', $foo);

# What I want: $foo = '1554;6554;5543'

?>

Does anyone know a preg replace I can use or any other method for doing this? These numbers are in a textarea, one number at each line.

Upvotes: 0

Views: 120

Answers (2)

Toto
Toto

Reputation: 91385

Don't put anchor in your regex:

preg_replace('/[\n\r]+/m', ';', $foo);

Upvotes: 0

bitWorking
bitWorking

Reputation: 12655

You don't need preg_replace for that. Try str_replace:

$foo = str_replace(array("\r", "\n", "\r\n"), ';', $foo);

Upvotes: 3

Related Questions