Parse through a string php and replace substrings

前端 未结 2 981
忘掉有多难
忘掉有多难 2021-01-23 06:00

I have a string, in PHP and the string has occurrences of the pattern %%abc%%(some substring)%%xyz%%

There are multiple occurrences of such substrings withi

2条回答
  •  孤街浪徒
    2021-01-23 06:21

    Use preg_replace_callback(), like this:

    preg_replace_callback( '#%%abc%%(.*?)%%xyz%%#', function( $match) {
        // Do some logic (with $match) to determine what to replace it with
        return 'replacement';
    }, $master_string);
    

提交回复
热议问题