gpt4 book ai didi

php preg_replace_callback block 引用正则表达式

转载 作者:搜寻专家 更新时间:2023-10-31 21:20:58 25 4
gpt4 key购买 nike

我正在尝试创建一个 REGEX

输入

> quote
the rest of it

> another paragraph
the rest of it

OUTPUT

quotethe rest of it

another paragraphthe rest of it

生成的 HTML

<blockquote>
<p>quote
the rest of it</p>
<p>another paragraph
the rest of it</p>
</blockquote>

下面是我的

$text = preg_replace_callback('/^>(.*)(...)$/m',function($matches){
return '<blockquote>'.$matches[1].'</blockquote>';
},$text);

DEMO

如有任何帮助或建议,我们将不胜感激

最佳答案

这是给定示例的可能解决方案。

$text = "> quote
the rest of it

> another paragraph
the rest of it";


preg_match_all('/^>([\w\s]+)/m', $text, $matches);

$out = $text ;
if (!empty($matches)) {
$out = '<blockquote>';
foreach ($matches[1] as $match) {
$out .= '<p>'.trim($match).'</p>';
}
$out .= '</blockquote>';
}

echo $out ;

输出:

<blockquote><p>quote 
the rest of it</p><p>another paragraph
the rest of it</p></blockquote>

关于php preg_replace_callback block 引用正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48845414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com