我正在尝试创建一个 REGEX
输入
> quote
the rest of it
> another paragraph
the rest of it
和OUTPUT
quote the rest of it
another paragraph the 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);
如有任何帮助或建议,我们将不胜感激
最佳答案
这是给定示例的可能解决方案。
$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/