首页 > 解决方案 > 在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。

标签: phpregexwordpressoopregexp-replace

解决方案


有几个问题。

  1. 你没有正确地声明你的类(它不是一个“函数”)。
  2. 之后有尾随空格== 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>'

推荐阅读