3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string = " 12| 150 | test one| 33235 13| 151| test two |33236 "; $pattern = '/(?:\s\|\s)|(?:\s\|)|(?:\|\s)/'; echo preg_replace($pattern, '|', $string);

preferences:
46.96 ms | 402 KiB | 5 Q