-1

code below gives a deprecation warning after upgrading to PHP 5.5+

$sentence=preg_replace('/~([^<>]{1,})~/e', "'<span class=\"helpstart\">'.UTF8_strtoupper('\\1').'</span>'", $sentence);

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in ..

How can I replace the code with preg_replace_callback()?

jones
  • 561
  • 3
  • 8
  • 19
  • The documentation is pretty clear, you just return the value you would want the match to be replaced with in a callback function : http://in3.php.net/preg_replace_callback – DhruvPathak Jan 28 '14 at 10:17
  • OK, thanks for your suggestion – jones Jan 28 '14 at 10:42
  • possible duplicate of [How to convert preg\_replace e to preg\_replace\_callback?](http://stackoverflow.com/questions/16367404/how-to-convert-preg-replace-e-to-preg-replace-callback) – Toto Jan 28 '14 at 13:58

2 Answers2

1
$sentence=preg_replace('/~([^<>]{1,})~/', function($match) {return "<span class=\"helpstart\">".UTF8_strtoupper($match)."</span>"; } , $sentence);

as per http://www.php.net/manual/en/function.preg-replace-callback.php

Alexander
  • 18,932
  • 15
  • 54
  • 138
0
$sentence=preg_replace_callback('/~([^<>]{1,})~/', function($match) {return "<span class=\"helpstart\">".UTF8_strtoupper($match[1])."</span>"; } , $sentence);

The first answer was an error on the function.

nKn
  • 13,452
  • 9
  • 38
  • 58