Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在wordpress中用html替换标记_Php_Regex_Wordpress_Oop_Regexp Replace - Fatal编程技术网

Php 在wordpress中用html替换标记

Php 在wordpress中用html替换标记,php,regex,wordpress,oop,regexp-replace,Php,Regex,Wordpress,Oop,Regexp Replace,我正在尝试替换自定义帖子中的所有wiki标记文本。例如: == My Subheading == === My sub Subheading === ==== another heading ==== 我正在尝试更改以下内容: 我的副标题我的副标题另一个 标题 所以,我尝试使用下面的函数。但是,没有成功 我看到: Parse error: syntax error, unexpected token "{", expecting "(" 我不太熟悉W

我正在尝试替换自定义帖子中的所有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;
    }
}
对内容执行功能{
私人$patterns,$replacements;
公共函数构造($analyze=false){
$this->patterns=array(
“/\r\n/”,
//标题
“/^====(.+?)=====$/m”,
“/^===(.+?)====$/m”,
“/^==(.+?)==$/m”,
//格式化
“/\'\'\'\'\'\'(.+?)\'\'\'\'\'\'\'\'\'\'\''\'\'\'\'\'(,
“/\'\'\'(.+?)\'\'\'\'\'/s”,
“/\'\'(.+?)\'\'\'/s”,
//特别的
“/^---+(\s*)$/m”,
“/\[\[(文件| img):((ht | f)tp(s?):\/\/(.+?)((.+)*\]\]/i”,
“/\[((新闻)(ht)f)tp(s?)| irc):\/\/(.+?)((.+)\]/i”,
“/\[((新闻)(ht)f)tp(s?)irc):\/\/(.+?)\]/i”,
//凹痕
“/[\n\r]:*。+([\n\r]:+。+)*/”,
“/^:(?!:)*(.+)$/m”,
“/([\n\r]:*。+)+/”,
“/^::*(.+)$/m”,
//有序列表
“/[\n\r]?#.+([\n|\r]#.+)+/”,
“/[\n\r]#(?!#)*(.+)([\n\r]#{2,}.+)+)/”,
//无序列表
“/[\n\r]?\*.+([\n\r]\*.+)+/”,
“/[\n\r]\*(?!\*)*(.+)([\n\r]\*{2,}.+)+)/”,
//清单项目
“/^[#\*]+*(.+)$/m”,

“/^(?!有一些问题

  • 您没有正确声明类(它不是“函数”)
  • ==我的副标题==
    之后有尾随空格(在行尾标记之前--您需要在
    $
    之前允许零个或多个空格)
  • 请教育自己——这将帮助您编写干净、一致和专业的代码

    代码:()


    有一些问题

  • 您没有正确声明类(它不是“函数”)
  • ==我的副标题==
    之后有尾随空格(在行尾标记之前--您需要在
    $
    之前允许零个或多个空格)
  • 请教育自己——这将帮助您编写干净、一致和专业的代码

    代码:()

    $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));
    
    '<h1>My Subheading</h1>
    <h2>My sub Subheading</h2>
    <h3>another heading</h3>'