The code is as follows:
$content = 'request:{"user": "yhm"},server:{"user": "yhm"}';
$content = preg_replace('/\{.+?\}/i', 'test', $content);
At present, all matching results are matched to test. I hope to pass all matching results:
json_encode('{"user": "yhm"}', JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
I can’t think of anything like this. Please give me some advice. Thank you.,You didn’t specify what kind of result you want, but through your second example, I guess you might want to do something like this:
- First case:
$content = 'request:{"user": "yhm"},server:{"user": "yhm"}'; $content = preg_replace_callback('/\{.+?\}/i', function ($match) { return json_encode($match[0], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); }, $content); echo $content;
Output result data structure:
request:"{\"user\": \"yhm\"}",server:"{\"user\": \"yhm\"}"
- The second case
$content = 'request:{"user": "yhm"},server:{"user": "yhm"}'; echo json_encode($content,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
Data result data structure:
"request:{\"user\": \"yhm\"},server:{\"user\": \"yhm\"}"
- The third case
$content = 'request:{"user": "yhm"},server:{"user": "yhm"}'; $content = '{' . preg_replace_callback('/([A-z0-9]+):/i', function ($match) { return json_encode($match[1], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . ":"; }, $content) . '}'; echo $content;
Output result data structure:
{"request":{"user": "yhm"},"server":{"user": "yhm"}}
- The fourth case
This case is based on the third case$content = 'request:{"user": "yhm"},server:{"user": "yhm"}'; $content = json_encode('{' . preg_replace_callback('/([A-z0-9]+):/i', function ($match) { return json_encode($match[1], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . ":"; }, $content) . '}', JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); echo $content;
Output result data structure:
"{\"request\":{\"user\": \"yhm\"},\"server\":{\"user\": \"yhm\"}}"
You didn’t specify what kind of result you want, but through your second example, I guess you might want to do something like this:
- First case:
$content = 'request:{"user": "yhm"},server:{"user": "yhm"}'; $content = preg_replace_callback('/\{.+?\}/i', function ($match) { return json_encode($match[0], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); }, $content); echo $content;
Output result data structure:
request:"{\"user\": \"yhm\"}",server:"{\"user\": \"yhm\"}"
- The second case
$content = 'request:{"user": "yhm"},server:{"user": "yhm"}'; echo json_encode($content,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
Data result data structure:
"request:{\"user\": \"yhm\"},server:{\"user\": \"yhm\"}"
- The third case
$content = 'request:{"user": "yhm"},server:{"user": "yhm"}'; $content = '{' . preg_replace_callback('/([A-z0-9]+):/i', function ($match) { return json_encode($match[1], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . ":"; }, $content) . '}'; echo $content;
Output result data structure:
{"request":{"user": "yhm"},"server":{"user": "yhm"}}
- The fourth case
This case is based on the third case$content = 'request:{"user": "yhm"},server:{"user": "yhm"}'; $content = json_encode('{' . preg_replace_callback('/([A-z0-9]+):/i', function ($match) { return json_encode($match[1], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . ":"; }, $content) . '}', JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); echo $content;
Output result data structure:
"{\"request\":{\"user\": \"yhm\"},\"server\":{\"user\": \"yhm\"}}"
Pure preg_ Can replace not meet the requirements?
First of all, you need to see which of these requirements you want. They all use prege_ replace
The first situation inspired me, thank you.
I guess. I can solve your problem.
And I don’t know what kind of result data you expect. I didn’t give an example
Then you use it directly
json_encode('{"user": "yhm"}', JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
Isn’t that good?Add the expected results
{“user”: “YHM”} is just an example. The actual JSON data is more complex than this.
The actual data may be as follows:{“action”:”register”,”act”:”bind_email”,”sid”:”698135766tRxFk”,”code”:”0.25360190368277435″,”client”:”h5″,”ver”:”1″,”passport”:”f7c2afa1ff56b720e8887d539ffb6f52″,”auth_code”:”2417dc”,”gid”:”5e9e8f11187c89.26546237″,”time_now”:”1648548866″}
You didn’t give the final result you want. First, see if the answer I gave you means this. You should give an original data structure and a converted data structure. Otherwise, where do you know what you want to do.
Is the formatted data. Look at JSON_ JSON of encode_ UNESCAPED_ UNICODE|JSON_ PRETTY_ Print these two parameters to understand what the younger brother is going to do.
Let me tell you the truth, your description is very abstract. I think you should be a product manager. I can only guess and write you an answer. Let’s see for yourself.
Can not give the actual data, involving trade secrets, can only give a rough idea, God, you can’t think of it dead! What else do I use?
Well, I mean the original data structure and the result data structure. Don’t you just assemble a custom data structure? It has nothing to do with trade secrets.