3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Function to create a dynamic replacement. * @param array $match An array with the match of the pattern. * @return string The replacement. */ function getReplacement($match) { if (isset($match[0])) { return $match[0].'<br/>'; } } //the value to replace specific parts. $string = 'Tip #1 some #12 random #123 text #1234, an #12345 example #123456'; //replace all parts starting with '#' and three numbers following. echo preg_replace_callback('/#[0-9]+/', 'getReplacement', $string);
Output for 5.5.0 - 5.5.35, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
Tip #1<br/> some #12<br/> random #123<br/> text #1234<br/>, an #12345<br/> example #123456<br/>

preferences:
146.75 ms | 404 KiB | 260 Q