0

I'm trying to replace the following string:

\u0080\u009cone hand rule\u0080\u009d

As I have to replace those codes for many strings and as there are various combinations of \u0080\u009 like \u00e2\u0080\u0099 and \u00a0 I ve searched for an solution to convert them all back into their according value.

The solution i ve found was posted on this site: http://www.avoid.org/replace-u-characters-in-json-string/

preg_replace("/\\\\u([a-f0-9]{4})/e", 
             "iconv('UCS-4LE','UTF-8', pack('V', hexdec('U$1')))",  
              json_encode($struct));

As i was trying to use this code php threw the following error:

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

Since I'm relativley new to php i bareley understand the preg_replace in first place but with the conversion to preg_replace_callback i'm totally overstrained. I've looked up How to convert preg_replace e to preg_replace_callback? but i didnt get it :/

Could somone pls explain how to convert my preg_replace into preg_replace_callback?

The code I m trying to get to work looks now like this:

$string = preg_replace("/\\\\u([a-f0-9]{4})",
              function($match){
                return = iconv('UCS-4LE','UTF-8',pack('V', hexdec('U$1')), $match[1]);                    
              }, 
              json_encode($string));

I ve read and understood how to access my found matches but since the

"iconv('UCS-4LE','UTF-8', pack('V', hexdec('U$1')))",

from the original code doesnt access any of the matches found I'm quiet confused how to set up my anonymous function

Community
  • 1
  • 1
Jan Raufelder
  • 287
  • 7
  • 23

1 Answers1

0

As of PHP 5.5.0 E_DEPRECATED level error is emitted when passing in the "\e" modifier. As of PHP 7.0.0 E_WARNING is emited in this case and "\e" modifier has no effect.

Please visit preg_replace and preg_replace_callback for documentation

"/\\u([a-f0-9]{4})" = > "/\\u([a-f0-9]{4})/"

김유걸
  • 3
  • 2
jameshwart lopez
  • 2,993
  • 6
  • 35
  • 65