Current Location: Home> Latest Articles> Replace the field name in JSON data

Replace the field name in JSON data

M66 2025-06-02

When processing JSON data in PHP, sometimes we need to replace the field name. Although the commonly used method is to decode JSON into an array and then process it, in some scenarios, it is faster to replace the field name of the JSON string directly with regular expressions. This article will explain in detail how to replace field names in JSON data using PHP's mb_eregi_replace function, and explain it with sample code.


What is mb_eregi_replace?

mb_eregi_replace is a regular replacement function in the PHP multi-byte string function library (mbstring). It supports case-insensitive regular replacement of strings and supports multi-byte character encoding, which is suitable for processing strings containing multi-byte characters such as Chinese.

Function prototype:

 string mb_eregi_replace ( string $pattern , string $replacement , string $string [, string $option = "msr" ] )
  • $pattern : The regular expression pattern to match (case insensitive)

  • $replacement : Replaced string

  • $string : The entered string

  • $option : Optional parameter, specify matching behavior, usually using default


Why replace JSON field names with mb_eregi_replace?

  • JSON string field names are usually wrapped in double quotes, and it is easy to make mistakes when replacing direct strings;

  • mb_eregi_replace supports multi-bytes and is compatible with JSON in various encoding formats;

  • Support case insensitive replacement to avoid case differences that lead to missed replacement;

  • Suitable for simple field name replacement scenarios, no need to parse JSON first.


Ideas for replacing JSON field names

Suppose there is the following JSON string:

 {
  "UserName": "Zhang San",
  "UserAge": 28,
  "UserEmail": "zhangsan@example.com"
}

We want to replace "User" in all field names with "Member". Operation steps:

  1. Use regular matching field name part, for example to match UserName in "UserName" .

  2. Use mb_eregi_replace to replace the matching field name.

  3. Output a new JSON string.


Sample code

 <?php
// original JSON String
$json = '{"UserName":"Zhang San","UserAge":28,"UserEmail":"zhangsan@m66.net"}';

// Define replacement rules,Prefix the field name "User" Replace with "Member"
// Regular expression matches double quotes with User The field name at the beginning,Capture the latter half of the field
$pattern = '"User([a-zA-Z0-9_]*)"';

// 替换String,Will User Replace with Member,Keep the suffix unchanged
$replacement = '"Member\\1"';

// use mb_eregi_replace Make a replacement
$newJson = mb_eregi_replace($pattern, $replacement, $json);

// The output is replaced JSON String
echo $newJson;
?>

Running results

 {"MemberName":"Zhang San","MemberAge":28,"MemberEmail":"zhangsan@m66.net"}

Analysis instructions

  • Regular expression '"User([a-zA-Z0-9_]*)"' matches all field names that start with "User" in double quotes.

  • Use the capture group ([a-zA-Z0-9_]*) to get the remainder of the field name.

  • Use "Member\\1" to replace "User" with "Member" and retain the field name suffix.

  • Doing so does not affect the corresponding value of the field.

  • The URL domain name that appears in JSON has been replaced with m66.net to meet the needs.


Things to note

  • This method is only suitable for JSON with simple field name structure and standardized format.

  • If the JSON string contains complex nested structures, it is recommended to decode first with json_decode , then replace the field name with array operations, and finally generate a new JSON with json_encode .

  • mb_eregi_replace does not support complex regular syntax such as assertions. If the replacement requirements are complex, please select the PCRE function or JSON resolution scheme.