本文介绍了从wordpress文章中拉出列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从我的wordpress文章中取出列表(ul)元素,这样我就可以将它放在不同的位置。
我当前的CSS拉出图像和blockqute并放置文本
html
<?php
$ content = preg_replace('/< blockquote>(。*?)< \ / blockquote> /','',get_the_content());
$ content = preg_replace('/(< img [^> *>)/','',$ content);
$ content = wpautop($ content); //添加段落标签
$ content = str_replace('< p>< / p>','',$ content); //删除空白段落
echo $ content;
?>
解决方案
只是一个友好的提示是它通常不被推荐用正则表达式解析html。
如果你想这样做,你可以尝试像这样:
$ pattern ='〜< ul> ;(。*?)< / UL>〜S';
所以在你的代码中它看起来像这样:
preg_match_all('/(〜< ul>(。*?)< / ul>〜s)/',$ content,$ ulElements);
然后将其从原始字符串中移除:
preg_replace('/(〜(。*?)< / ul>〜s)/','',$ content);
I want to pull out the list (ul) element from my wordpress post(s) so I can put it in a different location.
My current css pulls out the images and blockqute and puts just the text
html
<?php
$content = preg_replace('/<blockquote>(.*?)<\/blockquote>/', '', get_the_content());
$content = preg_replace('/(<img [^>]*>)/', '', $content);
$content = wpautop($content); // Add paragraph-tags
$content = str_replace('<p></p>', '', $content); // remove empty paragraphs
echo $content;
?>
解决方案
Just a friendly reminder is that it is generally not recommended to parse html with regex.If you would like to do that anyway you could try like this:
$pattern = '~<ul>(.*?)</ul>~s';
So in your code it would look like this:
preg_match_all('/(~<ul>(.*?)</ul>~s)/', $content, $ulElements);
And then for removing it from the original string:
preg_replace('/(~<ul>(.*?)</ul>~s)/', '', $content);
这篇关于从wordpress文章中拉出列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!