【问题标题】:Preg Replace in PHP for Heading Tags在 PHP 中 Preg 替换标题标签
【发布时间】:2014-10-08 10:23:50
【问题描述】:

我有一个降价文本内容,我必须在不使用库函数的情况下替换它。所以我为此使用了 preg 替换。它在某些情况下工作正常。对于标题等情况

    for eg Heading
           ======= 

should be converted to <h1>Heading</h1> and also

    ##Sub heading should be converted to <h2>Sub heading</h2>
    ###Sub heading should be converted to <h3>Sub heading</h3>

我试过了

$text = preg_replace('/##(.+?)\n/s', '<h2>$1</h2>', $text);

上面的代码有效,但我需要计算哈希符号,并基于此分配标题标签。

请大家帮帮我....

【问题讨论】:

    标签: php regex preg-replace markdown


    【解决方案1】:

    尝试使用preg_replace_callback。
    像这样 -

    $regex = '/(#+)(.+?)\n/s';
    $line = "##Sub heading\n ###sub-sub heading\n";
    $line = preg_replace_callback(
                $regex,
                function($matches){
                    $h_num = strlen($matches[1]);
                    return "<h$h_num>".$matches[2]."</h$h_num>";
                },
                $line
            );
    echo $line;
    

    输出会是这样的 -

    <h2>Sub heading</h2> <h3>sub-sub heading</h3>
    

    编辑
    对于将= 用于标题和# 用于子标题的组合问题,正则表达式变得有点复杂,但使用preg_replace_callback 的原理保持不变。
    试试这个 -

    $regex = '/(?:(#+)(.+?)\n)|(?:(.+?)\n\s*=+\s*\n)/';
    $line = "Heading\n=======\n##Sub heading\n ###sub-sub heading\n";
    $line = preg_replace_callback(
                $regex,
                function($matches){
                    //var_dump($matches);
                    if($matches[1] == ""){
                        return "<h1>".$matches[3]."</h1>";  
                    }else{
                        $h_num = strlen($matches[1]);
                        return "<h$h_num>".$matches[2]."</h$h_num>";
                    }   
                },
                $line
            );
    echo $line;
    

    谁的输出是 -

    <h1>Heading</h1><h2>Sub heading</h2> <h3>sub-sub heading</h3>
    

    【讨论】:

    • 错误的结束标签? =>
    • 感谢您的回答。但结果显示子标题 子子标题。如何删除
    • 对于严格相等,您可以简单地使用 preg_replace
    • 现在我使用下面的代码来表示等号。这是正确的方法吗...$sym1=""; $sym=substr_count($text, '=='); for($i=0;$i$1', $text); $text=str_replace('=','',$text);
    【解决方案2】:

    像这样做一个 preg_match_all:

    $string = "#####asdsadsad";
    preg_match_all("/^#/", $string, $matches);
    var_dump ($matches);
    

    根据匹配次数,您可以做任何您想做的事情。

    或者,使用preg_replace_callback 函数。

    $input = "#This is my text";
    $pattern = '/^(#+)(.+)/';
    $mytext = preg_replace_callback($pattern, 'parseHashes', $input);
    var_dump($mytext);
    
    function parseHashes($input) {
        var_dump($input);
        $matches = array();
        preg_match_all('/(#)/', $input[1], $matches);
        var_dump($matches[0]);
        var_dump(count($matches[0]));
        $cnt = count($matches[0]);
        if ($cnt <= 6 && $cnt > 0) {
            return '<h' . $cnt . ' class="if you want class here">' . $input[2] . '</h' . $cnt . '>';
        } else {
            //This is not a valid h tag. Do whatever you want.
            return false;
        }
    }
    

    【讨论】:

    • 我不知道如何使用preg_replace_callback。你能给我一个例子吗..
    • 上面的代码只显示了length=1 0 => string '#' (length=1)。我怎样才能得到哈希符号的个数
    • 你的回答也不错。但确切的简单方法是 Kamehameha 的上述回答。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    相关资源
    最近更新 更多