使用preg_replace的多个答案

I have a problem with preg_replace in PHP.

My text:

[Derp] a
• [Derp] a

My regex:

$simple_search[0] = '/\[(.*?)\] (.*?)/is';
$simple_search[1] = '/\• \[(.*?)\] (.*?)/is';

My subject:

$simple_replace[0] = "[color=#009D9D][$1][/color] $2";
$simple_replace[1] = "[color=#30BA76]• [$1][/color] [color=#92CF91]$2[/color]";

After preg_replace:

[color=#009D9D][Derp][/color] a
[color=#30BA76]#color=#009D9D][Derp][/color[/color] [color=#606090]: [/color]a

(it's a tool for coloring quotes)

[Derp] a and • [Derp] a must not have the same color.

The problem is that the first search then replaces that this is not the right thing.

How can I detect that research is equal to the string?

replace your first regexp:

/(?<!\• )\[(.*?)\] (.*?)/is

means can not have front of the "[" an "•" and a space. Also if the • stands in beginning of your lines then you could put ^ front of it

        $str = '[Derp] a
                • [Derp] a';

        $simple_search[0] = '/(\• )?(?P<m2>\[.*?\]) (?P<m3>.*)/i';

        echo $str = preg_replace_callback($simple_search[0],
            function ($m) {
                if (!$m[1]) return '[color=#009D9D]' . $m[2] . '[/color] ' . $m[3];
                else return '[color=#30BA76]• ' . $m[2] . '[/color] [color=#92CF91]' . $m[3] . '[/color]';
            }, $str
        );

result

[color=#009D9D][Derp][/color] a
[color=#30BA76]• [Derp][/color] [color=#92CF91]a[/color]