【问题标题】:Replace markdown with html in wordpress在wordpress中用html替换markdown
【发布时间】: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。

【问题讨论】:

    标签: php regex wordpress oop regexp-replace


    【解决方案1】:

    有一些问题。

    1. 您没有正确声明您的类(它不是“函数”)。
    2. == 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>'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-23
      • 2023-03-21
      • 1970-01-01
      • 2019-11-05
      • 2010-10-02
      • 2022-12-03
      • 1970-01-01
      • 2018-12-04
      相关资源
      最近更新 更多