3v4l.org

run code in 300+ PHP versions simultaneously
<?php class php { /** * str_replace() from the end of a string that can also be limited e.g. replace only the last instance of '</div>' with '' * * @param string $find * @param string $replace * @param string $subject * @param int $replacement_limit | -1 to replace all references * * @return string */ public static function str_replace($find, $replace, $subject, $replacement_limit = -1) { $find_pattern = str_replace('/', '\/', $find); return preg_replace('/' . $find_pattern . '/', $replace, $subject, $replacement_limit); } /** * str_replace() from the end of a string that can also be limited e.g. replace only the last instance of '</div>' with '' * * @param string $find * @param string $replace * @param string $subject * @param int $replacement_limit | -1 to replace all references * * @return string */ public static function str_rreplace($find, $replace, $subject, $replacement_limit = -1) { return strrev( self::str_replace(strrev($find), strrev($replace), strrev($subject), $replacement_limit) ); } } $find = '$'; $replace = 'dollar'; $subject = 'This is a $ symbol. I like making $s.'; echo php::str_replace($find, $replace, $subject);

preferences:
27.31 ms | 410 KiB | 6 Q