I am currently re-programming a framework in php and I upgraded our development server to php 5.5.3. When I fire up the webbrowser, it returns the following error:
[19-Oct-2013 16:54:05 Europe/Amsterdam] PHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /Applications/MAMP/htdocs/fw/lib/lang.php on line 57
And line 57 is;
$response = preg_replace('/\{'.$find.'(:(.*))?\}/Ue', 'str_replace("{value}", "\\2", $replace)', $response);
I am really horrible at reading these php documentation, I am a beginner and simply changing preg_replace() by preg_replace_callback() is too good to be true. A co-worker told me it had to be something like $value[1] but that didn't work eather.
Is there a simple solution, am I overlooking something?
Here is the page about modifiers, giving you more detail about why it is deprecated, and what that means exactly.
Basically the reason is that the /e modifier causes a string to be evaluated as PHP code, as if eval was called. Using preg_replace_callback instead, which allows you to pass it an actual PHP function is indeed the way to go.
If you replace the string-code (second parameter) by an anonymous function, your code should then look something like this:
$response = preg_replace_callback(
'/\{'.$find.'(:(.*))?\}/U',
function($m) use ($replace) {
return str_replace($m, "\\2", $replace);
} ,
$response);
The keyword use makes sure that the anonymous function can use the $replace variable which should be defined in the calling scope. See this discussion.
Related
I've got some code that uses create_function, which is now deprecated. The comments for create_function say * #deprecated 7.2 Use anonymous functions instead.
This is the function itself: create_function ('$matches', 'return strtoupper("\0");')
And this is the context:
$match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./',
create_function ('$matches', 'return strtoupper("\0");'), strtolower(trim($match[1])));
My code isn't running because it's been deprecated, so I want to turn it into an anonymous function instead. I'm not entirely sure what this bit of code does, so I don't really have a reliable way of testing it, but I THINK that create_function block can be replaced with
function($matches) {
return strtoupper($matches);
}
Would that work? Does that do the same thing that the create_function function does? I'm not sure what "\0" does in that context, so I'm hoping I can get some advice.
I found a similar question another place and tried the answer there. Turns out it's equivalent to this function:
function($m) {
return strtoupper($m[0]);
}
Found it here: Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback
thanks to #Philipp
I'm getting error with Symfony 1.4 Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /vagrant/lib/util/sfToolkit.class.php on line 365
The function concerned is below:
public static function pregtr($search, $replacePairs)
{
if(strpos(implode(', ', $replacePairs),'e') > 0)
print_r($replacePairs);
return preg_replace(array_keys($replacePairs),array_values($replacePairs), $search);
}
The values concerned is:
Array
(
[#/(.?)#e] => '::'.strtoupper('\1')
[/(^|_|-)+(.)/e] => strtoupper('\2')
)
How can I change this to what is equivalent using preg_replace_callback?
You can try to use the sf 1.5. version maintained by guys from L'Express: https://github.com/LExpress/symfony1 (they have fixed the problem)
If their changes are too much you can check how they managed to solve the 'e modifier' problem and copy their solution. Searching the code for use of pregtr it looks that there are only three places where the e modifier is used (in lib/util/sfInflector.class.php and in filter form classes in the Doctrine and Proper plugins). It's not that difficult to fix the code by yourself in those places.
I attempted the following code:
$for_callback=create_function('$match','return $GLOBALS[\'replacements\'][$match[1]];');
$result = preg_replace_callback( $regex, '$for_callback', $string);
The variable $GLOBALS['replacements'] is generated dynamically before this function is called.
I get an error message like
Warning: preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, '$for_callback', to be a valid callback in...
created functions and callbacks are both new to me. This is growing out of some code given to me by nickb at My question about preg_replace that turned into preg_replace_callback.
What I'm trying to do is wrap the code in that answer into a function and I'm running into errors with scope avoiding re-defining a function. (upgrading to PHP 5.3+ is a remote posibility option for me at the moment.)
How do I get this to work?
First, variables must not be enclosed with single quotes, as they will not be replaced with the real value.
And second, you should use anonymous functions (i.e. closures) instead as they are much easier. Use them like in this example:
$for_callback = function($match) {
return $GLOBALS['replacements'][$match[1]];
};
$result = preg_replace_callback( $regex, $for_callback, $string);
edit: Closures became available in PHP 5.3. So if you are still using PHP < 5.3 you should (really update or) use the following:
$for_callback=create_function('$match','return $GLOBALS[\'replacements\'][$match[1]];');
$result = preg_replace_callback( $regex, $for_callback, $string);
This does not work, and outputs an empty string:
$check["pattern"] = "correct";
$text = "Could this be correct?";
echo preg_replace_callback($check["pattern"],ucfirst,$text);
Would have been nice to use built in functions. In fact general callbacks do allow built in functions as per http://php.net/manual/en/language.types.callable.php, but not preg_replace_callback. Could be a feature request for php?
Your code should trigger a notice and a warning:
Notice: Use of undefined constant ucfirst - assumed 'ucfirst'
Warning: preg_replace_callback(): Delimiter must not be alphanumeric or backslash
If it doesn't, you seriously need to check your PHP error reporting settings. Fixing the code with the help of the error messages:
$check["pattern"] = "/correct/";
$text = "Could this be correct?";
echo preg_replace_callback($check["pattern"],'ucfirst',$text);
... we get this:
Warning: ucfirst() expects parameter 1 to be string, array given
So using a builtin callback function is working fine. However, as the manual page for ucfirst() explains, the function expects a string, not an array. And, as the manual page for preg_replace_callback() explains:
A callback that will be called and passed an array of matched elements in the subject string.
To sum up: it isn't a sensible feature request, it's a bug in your code ;-)
It works just fine with all functions. The problem is that those functions expect certain parameters. ucfirst expects strings as input, but preg_replace_callback is passing an array of matches.
So... if you have a built-in function whose signature is compatible with a preg_replace callback signature, it works. But no, not all built-in functions have a compatible signature.
$stringText = "[TEST-1] test task 1 Created: 06/Apr/11 Updated: 06/Apr/11";
$splitArray = split(" ",$stringText);
Deprecated: Function split() is deprecated in C:\wamp\www\RSS.php on line 27
Why this error happen ?
http://php.net/manual/en/function.split.php
From the manual
Warning This function has been
DEPRECATED as of PHP 5.3.0. Relying on
this feature is highly discouraged
Note:
As of PHP 5.3.0, the regex extension
is deprecated in favor of the PCRE
extension. Calling this function will
issue an E_DEPRECATED notice. See the
list of differences for help on
converting to PCRE.
I guess you're supposed to use the alternative preg_split(). Or if you're not using a regex, just use explode
split has been replaced with explode, see http://php.net/explode for more information. Works the same as split, but split is 'deprecated' basically means that is a old function that shouldn't be used anymore, and is not likely to be in later versions of php.
Use following explode function:
$command = explode(" ", $tag[1]);
This is the standard solution for this case.
Its Perfectly working.
Ahh, the docs says about it. And the docs also say which functions should be used instead of this:
preg_split
explode
str_split
Because the function has been deprecated? You can customize the error_reporting level to not log / display the depreciated errors. But it would be more prudent to just correct the issue (IE use explode instead for the simple split you are doing above.)
You can use this custom function for old codes:
if (!function_exists('split')) {
function split($pattern, $subject, $limit=-1, $flags=0){
return preg_split($pattern, $subject, $limit, $flags);
}
}