在php中替换textarea linebreaks

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+|
||\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.

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

$foo = str_replace(array("", "
", "
"), ';', $foo);

Don't put anchor in your regex:

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