Php,Regex preg_replace_callback(Php, Regex preg_replace_callback)

我正在使用preg_replace_callback ,这是我想要做的:

$result = '[code]some code here[/code]'; $result = preg_replace_callback('/\[code\](.*)\[\/code\]/is', function($matches){ return '<div>'.trim($matches[1]).'</div>'; }, $result);

我们的想法是将[code]的每个匹配替换为<div> ,将[/ code]替换为</div> ,并修改它们之间的代码。

问题是这个字符串例如:

$result = '[code]some code[/code]some text[code]some code[/code]';

我希望结果有两个分开的 div:

$result = '<div>some code</div>some text<div>some code</div>';

我得到的结果是:

$result = '<div>some code[code]some text[/code]some code</div>';

现在我知道原因了,我理解正则表达式,但我无法提出解决方案,如果有人知道如何使它工作,我将非常感谢,谢谢大家,祝你有愉快的一天。

I am using preg_replace_callback, Here is what i am trying to do:

$result = '[code]some code here[/code]'; $result = preg_replace_callback('/\[code\](.*)\[\/code\]/is', function($matches){ return '<div>'.trim($matches[1]).'</div>'; }, $result);

The idea is to replace every match of [code] with <div> and [/code] with </div>, And trim the code between them.

The problem is with this string for example:

$result = '[code]some code[/code]some text[code]some code[/code]';

What i want the result to have 2 separated div's:

$result = '<div>some code</div>some text<div>some code</div>';

The result i get is:

$result = '<div>some code[code]some text[/code]some code</div>';

Now i know the reason, And i understand the regex but i couldn't come up with solution, If anyone know how to make it work i will be very thankful, Thank you all and have a nice day.

最满意答案

你的问题是贪婪的匹配:

/\[code\](.*?)\[\/code\]/is

应该按照你的意愿行事。

正则表达式重复是贪婪的 ,这意味着它可以捕获尽可能多的匹配项,然后如果它发现它与重复后剩下的不匹配则一次放弃一个匹配。 通过使用问号,您表示您想要非贪婪或懒惰地匹配,这意味着引擎将尝试匹配正则表达式FIRST的其余部分,然后增加重复的大小。

Your problem is greedy matchiing:

/\[code\](.*?)\[\/code\]/is

Should behave as you want it to.

Regex Repetition is greedy, which means it captures as many matching items as it can, then gives up one match at a time if it finds that it can't match what's left after the repetition. By using a question mark, you indicate that you want to match non-greedily, or lazily, meaning that the engine will try to match the rest of the regular expression FIRST, then grow the size of the repetition after.

Php,Regex preg_replace_callback(Php, Regex preg_replace_callback)

我正在使用preg_replace_callback ,这是我想要做的:

$result = '[code]some code here[/code]'; $result = preg_replace_callback('/\[code\](.*)\[\/code\]/is', function($matches){ return '<div>'.trim($matches[1]).'</div>'; }, $result);

我们的想法是将[code]的每个匹配替换为<div> ,将[/ code]替换为</div> ,并修改它们之间的代码。

问题是这个字符串例如:

$result = '[code]some code[/code]some text[code]some code[/code]';

我希望结果有两个分开的 div:

$result = '<div>some code</div>some text<div>some code</div>';

我得到的结果是:

$result = '<div>some code[code]some text[/code]some code</div>';

现在我知道原因了,我理解正则表达式,但我无法提出解决方案,如果有人知道如何使它工作,我将非常感谢,谢谢大家,祝你有愉快的一天。

I am using preg_replace_callback, Here is what i am trying to do:

$result = '[code]some code here[/code]'; $result = preg_replace_callback('/\[code\](.*)\[\/code\]/is', function($matches){ return '<div>'.trim($matches[1]).'</div>'; }, $result);

The idea is to replace every match of [code] with <div> and [/code] with </div>, And trim the code between them.

The problem is with this string for example:

$result = '[code]some code[/code]some text[code]some code[/code]';

What i want the result to have 2 separated div's:

$result = '<div>some code</div>some text<div>some code</div>';

The result i get is:

$result = '<div>some code[code]some text[/code]some code</div>';

Now i know the reason, And i understand the regex but i couldn't come up with solution, If anyone know how to make it work i will be very thankful, Thank you all and have a nice day.

最满意答案

你的问题是贪婪的匹配:

/\[code\](.*?)\[\/code\]/is

应该按照你的意愿行事。

正则表达式重复是贪婪的 ,这意味着它可以捕获尽可能多的匹配项,然后如果它发现它与重复后剩下的不匹配则一次放弃一个匹配。 通过使用问号,您表示您想要非贪婪或懒惰地匹配,这意味着引擎将尝试匹配正则表达式FIRST的其余部分,然后增加重复的大小。

Your problem is greedy matchiing:

/\[code\](.*?)\[\/code\]/is

Should behave as you want it to.

Regex Repetition is greedy, which means it captures as many matching items as it can, then gives up one match at a time if it finds that it can't match what's left after the repetition. By using a question mark, you indicate that you want to match non-greedily, or lazily, meaning that the engine will try to match the rest of the regular expression FIRST, then grow the size of the repetition after.