【发布时间】:2015-06-09 21:07:57
【问题描述】:
我正在尝试弄清楚如何在 Wordpress 帖子中创建一个称为“pullquote”的通用报纸设备。 (但这不是严格意义上的 Wordpress 问题;它更像是一个通用的正则表达式问题。)我有一个标签来围绕帖子中的文本。我想复制标签之间的文本(我知道该怎么做)并将其插入到帖子中 p 标签的第 3 和第 4 个实例之间。
下面的函数查找文本并去除标签,但只是将匹配的文本添加到开头。我需要帮助定位第 3/4 段
或者...也许我想错了。也许有一些方法可以像使用 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 实际上将换行符转换为
个字符。大多数 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 以使用换行符而不是
并弄清楚如何从返回的字符串中去除那些换行符。感谢到目前为止的所有帮助!
【问题讨论】:
-
请用输入和所需输出更新您的问题。
标签: regex wordpress preg-replace