在wordpress中用html替换markdown
Posted
技术标签:
【中文标题】在wordpress中用html替换markdown【英文标题】:Replace markdown with html in wordpress 【发布时间】:2021-03-21 14:44:57 【问题描述】:我正在尝试替换自定义帖子中的所有 wiki 降价文本。示例:
== My Subheading ==
=== My sub Subheading ===
==== another heading ====
我正在尝试更改该内容,如下所示:
我的副标题
我的副标题
另一个 标题
所以,我尝试使用以下功能。但是,没用!
我看到了:
Parse error: syntax error, unexpected token "", expecting "("
我对 WP 自定义功能不太熟悉。你们能帮帮我吗?
function the_content
private $patterns, $replacements;
public function __construct($analyze=false)
$this->patterns=array(
"/\r\n/",
// Headings
"/^==== (.+?) ====$/m",
"/^=== (.+?) ===$/m",
"/^== (.+?) ==$/m",
// Formatting
"/\'\'\'\'\'(.+?)\'\'\'\'\'/s",
"/\'\'\'(.+?)\'\'\'/s",
"/\'\'(.+?)\'\'/s",
// Special
"/^----+(\s*)$/m",
"/\[\[(file|img):((ht|f)tp(s?):\/\/(.+?))( (.+))*\]\]/i",
"/\[((news|(ht|f)tp(s?)|irc):\/\/(.+?))( (.+))\]/i",
"/\[((news|(ht|f)tp(s?)|irc):\/\/(.+?))\]/i",
// Indentations
"/[\n\r]: *.+([\n\r]:+.+)*/",
"/^:(?!:) *(.+)$/m",
"/([\n\r]:: *.+)+/",
"/^:: *(.+)$/m",
// Ordered list
"/[\n\r]?#.+([\n|\r]#.+)+/",
"/[\n\r]#(?!#) *(.+)(([\n\r]#2,.+)+)/",
// Unordered list
"/[\n\r]?\*.+([\n|\r]\*.+)+/",
"/[\n\r]\*(?!\*) *(.+)(([\n\r]\*2,.+)+)/",
// List items
"/^[#\*]+ *(.+)$/m",
"/^(?!<li|dd).+(?=(<a|strong|em|img)).+$/mi",
"/^[^><\n\r]+$/m",
);
$this->replacements=array(
"\n",
// Headings
"<h3>$1</h3>",
"<h2>$1</h2>",
"<h1>$1</h1>",
//Formatting
"<strong><em>$1</em></strong>",
"<strong>$1</strong>",
"<em>$1</em>",
// Special
"<hr/>",
"<img src=\"$2\" alt=\"$6\"/>",
"<a href=\"$1\">$7</a>",
"<a href=\"$1\">$1</a>",
// Indentations
"\n<dl>$0\n</dl>",
"<dd>$1</dd>",
"\n<dd><dl>$0\n</dl></dd>",
"<dd>$1</dd>",
// Ordered list
"\n<ol>\n$0\n</ol>",
"\n<li>$1\n<ol>$2\n</ol>\n</li>",
// Unordered list
"\n<ul>\n$0\n</ul>",
"\n<li>$1\n<ul>$2\n</ul>\n</li>",
// List items
"<li>$1</li>",
// Newlines
"$0<br/>",
"$0<br/>",
);
if($analyze)
foreach($this->patterns as $k=>$v)
$this->patterns[$k].="S";
public function parse($input)
if(!empty($input))
$output=preg_replace($this->patterns,$this->replacements,$input);
else
$output=false;
return $output;
我主要是尝试在 the_content 上使用过滤器,它会使用正则表达式替换将降价文本转换为简单的 html。
【问题讨论】:
【参考方案1】:有一些问题。
-
您没有正确声明您的类(它不是“函数”)。
== My Subheading ==
之后有尾随空格(行标记之前 -- 您需要在 $
之前允许零个或多个空格)
请自学php's PSR-12 coding standards——这将帮助您编写干净、一致和专业的代码。
代码:(Demo)
$text = <<<TEXT
== My Subheading ==
=== My sub Subheading ===
==== another heading ====
TEXT;
class ContentParser
private $patterns = [];
private $replacements = [];
public function __construct()
$this->patterns = [
// Headings
"/^==== (.+?) ====\h*$/m",
"/^=== (.+?) ===\h*$/m",
"/^== (.+?) ==\h*$/m",
];
$this->replacements = [
// Headings
"<h3>$1</h3>",
"<h2>$1</h2>",
"<h1>$1</h1>",
];
public function parse($input)
if (!empty($input))
$output = preg_replace($this->patterns, $this->replacements, $input);
else
$output = false; // I don't recommend returning a boolean when otherwise returning strings
return $output;
$object = new ContentParser();
var_export($object->parse($text));
输出:(单引号来自var_export()
,你可以用echo
代替)
'<h1>My Subheading</h1>
<h2>My sub Subheading</h2>
<h3>another heading</h3>'
【讨论】:
以上是关于在wordpress中用html替换markdown的主要内容,如果未能解决你的问题,请参考以下文章