php替换括号里面某些字符串

public function testReplace() {
    $post_xml_data='asdsa&(123123)';
    // dd(preg_replace('/&(?!#?[a-z0-9]+;)/', '&', $post_xml_data));

    $subject = 'asdsa&(123123)
    
    ';
    $new = preg_replace_callback('/(\([^\)]*\))/', function($matches){
        dump($matches);
        return str_replace(['<','>'], ['<','>'], $matches[0]);
    }, $subject);
    dd($new);
}

php替换括号里面某些字符串_第1张图片
还有一个加强版的函数,接收一组正则作为键,回调函数作为值的批量替换:

$subject = 'Aaaaaa Bbb';

preg_replace_callback_array(
    [
        '~[a]+~i' => function ($match) {
            echo strlen($match[0]), ' matches for "a" found', PHP_EOL;
        },
        '~[b]+~i' => function ($match) {
            echo strlen($match[0]), ' matches for "b" found', PHP_EOL;
        }
    ],
    $subject
);

你可能感兴趣的:(php)