3v4l.org

run code in 300+ PHP versions simultaneously
<?php $before = html_entity_decode("&nbsp;") . "&nbsp;abc&nbsp;xyz&nbsp;"; $pattern = '~^(\xC2\xA0|&nbsp;)*(.*?)(?1)*$~'; $after = preg_replace($pattern, '$2', $before); var_dump($before, $after);
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.13
string(26) " &nbsp;abc&nbsp;xyz&nbsp;" string(12) "abc&nbsp;xyz"

preferences:
70.33 ms | 406 KiB | 5 Q