我正在尝试弄清楚如何在 WordPress 帖子中创建一个名为“pullquote”的常见报纸设备。 (但这并不是严格意义上的 WordPress 问题;它更像是一个通用的正则表达式问题。)我有一个标签来包围帖子中的文本。我想复制标签之间的文本(我知道该怎么做)并将其插入帖子中 p 标签的第三个和第四个实例之间。
下面的函数查找文本并删除标签,但只是将匹配的文本添加到开头。我需要帮助定位第三/第四段
或者...也许我的想法是错误的。也许有某种方法可以像使用 jQuery nth-child 那样定位元素?
帖子:
<p>If you wanna improve yer German, don't try to read Heine or some elevated crap... watch old episodes of [callout]Tatort or Bukow & Konig[/callout].</p>
<p>If I were teaching a music appreciation I wouldn't teach Beethoven. I'd teach Stamitz and average composers.</p>
<p>And here is a 3rd paragraph.</p>
<p>And here is a 4th paragraph.</p>
期望的结果
<p>If you wanna improve yer German, don't try to read Heine or some elevated crap... watch old episodes of Tatort or Bukow & Konig.</p>
<p>If I were teaching a music appreciation I wouldn't teach Beethoven. I'd teach Stamitz and average composers.</p>
<p>And here is a 3rd paragraph.</p>
<blockquote class="pullquote">Tatort or Bukow & Konig</blockquote>
<p>And here is a 4th paragraph.</p>
到目前为止,这就是我的代码:
function jchwebdev_pullquote( $content ) {
$newcontent = $content;
$replacement = '$1';
$matches = array();
$pattern = "~\[callout\](.*?)\[/callout\]~s";
// strip out 'shortcode'
$newcontent = preg_replace($pattern, $replacement, $content);
if( preg_match($pattern, $content, $matches)) {
// now have formatted pullquote
$pullquote = '<blockquote class="pullquote">' .$matches[1] . '</blockquote>';
// now how do I target and insert $pullquote
// between 3rd and 4th paragraph?
preg_replace($3rd_4th_pattern, $3rd_4th_replacement,
$newcontent);
return $newcontent;
}
return $content;
}
add_filter( 'the_content' , 'jchwebdev_pullquote');
编辑:我想修改我的问题,使其更具体一点。 WordPress 实际上将换行符转换为
字符。大多数 WordPress 帖子甚至不使用明确的“p”标签,因为它们是不需要的。到目前为止,解决方案的问题在于它们似乎删除了换行符,因此如果帖子(源文本)有换行符,它看起来很奇怪。
典型的现实世界 WordPress 帖子:
If you wanna improve yer German, don't try to read Heine or some elevated crap... watch old episodes of [callout]Tatort or Bukow & Konig[/callout].
If I were teaching a music appreciation I wouldn't teach Beethoven. I'd teach Stamitz and average composers.
And here is a 3rd paragraph.
And here is a 5th paragraph.
Wordpress 的渲染方式如下:
<p>If you wanna improve yer German, don't try to read Heine or some elevated crap... watch old episodes of [callout]Tatort or Bukow & Konig[/callout].</p>
<p>If I were teaching a music appreciation I wouldn't teach Beethoven. I'd teach Stamitz and average composers.</p>
<p>And here is a 3rd paragraph.</p>
<p></p>
<p>And here is a 5th paragraph.</p>
因此,在完美的世界中,我想采用“典型的现实世界帖子”并让 preg_replace 将其渲染为:
If you wanna improve yer German, don't try to read Heine or some elevated crap... watch old episodes of Tatort or Bukow & Konig.
If I were teaching a music appreciation I wouldn't teach Beethoven. I'd teach Stamitz and average composers.
And here is a 3rd paragraph.
<blockquote class="callout">Tatort or Bukow & Konig</blockquote>
And here is a 5th paragraph.
...Wordpress 将呈现为:
<p>If you wanna improve yer German, don't try to read Heine or some elevated crap... watch old episodes of Tatort or Bukow & Konig.</p>
<p>If I were teaching a music appreciation I wouldn't teach Beethoven. I'd teach Stamitz and average composers.</p>
<p>And here is a 3rd paragraph.</p>
<blockquote class="callout">Tatort or Bukow & Konig</blockquote>
<p>And here is a 5th paragraph.</p>
也许这已经离谱太远了,我应该在 WordPress 论坛中重新发布,但我认为我需要的是一种改变 preg_replace 的方法,以使用换行符作为分隔符而不是
并弄清楚如何从返回的字符串中删除那些换行符。感谢迄今为止提供的所有帮助!
最佳答案
如果想使用PHP HTML/XML解析,请引用How do you parse and process HTML/XML in PHP? .
对于正则表达式解决方案,这里是一个正则表达式解决方案:
查找: (?s)((?:<p>.*?<\/p>\s*){3})
这个正则表达式只会捕获前 3 个 <p>
标签,然后在它们后面添加一个节点。
替换: $1<blockquote class="pullquote">Tatort or Bukow & Konig</blockquote>\n
代码:
$re = "/(?s)((?:<p>.*?<\\/p>\\s*){3})/";
$str = "<p>If you wanna improve yer German, don't try to read Heine or some elevated crap... watch old episodes of [callout]Tatort or Bukow & Konig[/callout].</p>\n<p>If I were teaching a music appreciation I wouldn't teach Beethoven. I'd teach Stamitz and average composers.</p>\n<p>And here is a 3rd paragraph.</p>\n<p>And here is a 4th paragraph.</p>";
$subst = "$1<blockquote class=\"pullquote\">Tatort or Bukow & Konig</blockquote>\n";
$result = preg_replace($re, $subst, $str, 1);
关于regex - 如何使用 preg_replace 在第三段和第四段之间插入文本字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29454837/