DWQA QuestionsCategory: Artificial Intelligencephp preg_ How can replace process all matched results as one action instead of replacing all with the same result?
Little seven brother asked 2 days ago

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:

  1. 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\"}"

  2. 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\"}"

  3. 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"}}

  4. 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\"}}"

kumfo replied 3 days ago

And I don’t know what kind of result data you expect. I didn’t give an example

kumfo replied 3 days ago

Then you use it directlyjson_encode('{"user": "yhm"}', JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);Isn’t that good?

kumfo replied 3 days ago

Add the expected results

kumfo replied 3 days ago

{“user”: “YHM”} is just an example. The actual JSON data is more complex than this.

kumfo replied 3 days ago

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″}

kumfo replied 3 days ago

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.

kumfo replied 3 days ago

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.

kumfo replied 3 days ago

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.

kumfo replied 3 days ago

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?

kumfo replied 3 days ago

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.

1 Answers
kumfo answered 3 days ago

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:

  1. 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\"}"

  2. 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\"}"

  3. 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"}}

  4. 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\"}}"

Little seven brother replied 3 days ago

Pure preg_ Can replace not meet the requirements?

kumfo replied 3 days ago

First of all, you need to see which of these requirements you want. They all use prege_ replace

Little seven brother replied 3 days ago

The first situation inspired me, thank you.

kumfo replied 3 days ago

I guess. I can solve your problem.