As php.net refers to documenation using the e modifier is not recommended,
This function has been DEPRECATED since PHP 5.5.0. Based on this feature, it is highly discouraged.
therefore, we better achieve our goal without using this modifier.
Here the solution is based on modern PHP tools:
$source = 'a_b__c___d____e'; echo preg_replace_callback( "%(_{2,})%i", function($matches) {return str_repeat( "-", strlen($matches[1]) ); }, $source );
Even with e , which is still available in our PHP environment, it is usually better to use a callback function - thanks to the callback, we avoid the rather insecure combination of the addslashes() function and the string evaluation, since running preg_replace with the specified modifier involves both actions at that time.
A preg_replace_callback available since version 4.0.5, but function($matches) {} is an anonymous function, which is actually a much newer language function, to run this code you need PHP in version 5.3.0 or later .
source share