php - 在wordpress中用html替换markdown
问题描述
我正在尝试替换我的自定义帖子中的所有 wiki markdown 文本。例子:
== 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。
解决方案
有几个问题。
- 你没有正确地声明你的类(它不是一个“函数”)。
- 之后有尾随空格
== My Subheading ==
(在行标记结束之前 - 您需要在之前允许零个或多个空格$
)
请自学PHP 的 PSR-12 编码标准——这将帮助您编写干净、一致和专业的代码。
代码:(演示)
$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));
输出:(单引号是 from var_export()
,你可以echo
改用)
'<h1>My Subheading</h1>
<h2>My sub Subheading</h2>
<h3>another heading</h3>'
推荐阅读
- java - 用户使用按钮向 tableview 输入数据
- spring - Spring Cloud Stream 3 RabbitMQ 消费者不工作
- html - 如何使用 .stroke1 类将点(SVG)居中
- java - 查找数组大小为 k 的窗口的最高频率的方法
- c# - 是否可以将 EF Core 配置为在保存时忽略子属性,使用外键创建关系,但在检索时仍填充子属性?
- reactjs - 当 React 应用程序在 Web 上运行时运行 express 服务器
- vba - 将多个字段发送到 Access VBA 中的新文件夹时出现无效的限定符编译错误
- flutter - Flutter 移动视觉中的过滤值
- javascript - 虽然有时它有效,但我在我的 React 项目中得到“.filter 不是函数”
- python - Numpy argsort - 发生了什么?