- preg_replace_callback: documentation ( source)
- str_replace: documentation ( source)
<?php
function replace_spaces ($text) {
function replacement ($matches) {
return str_replace ($matches[1], ' ', ' ').' ';
}
return preg_replace_callback ('/( +) /', replacement, $text);
}
echo replace_spaces('hello world');