【问题标题】:wordpress preg_match_all() error, use preg_replace_callbackwordpress preg_match_all() 错误,使用 preg_replace_callback
【发布时间】:2020-06-07 00:02:59
【问题描述】:

我必须使用 preg_replace_callback 函数而不是 preg_match_all 函数,但是我对这个问题的研究没有产生任何结果。请你能帮我解决下面的代码吗?

$content_post = get_post($id);

$content = $content_post->post_content;

$html_content = wpautop( $content );

preg_match_all('#<!--part_name:(.*?)-->#Ssie',$html_content,$PartCount);

preg_match_all('#<!--part_name:.*?-->(.*?)<!--nextpage-->#Ssie',$html_content,$PartEmbed);

【问题讨论】:

    标签: php wordpress function


    【解决方案1】:

    preg_replace_callback() 是一个棘手的函数。它的文档在这里:https://www.php.net/manual/en/function.preg-replace-callback.php 这是一个示例:

    $oldString ='
    <!--part_name:PartName11-->
    Description of the part etc...
    <br>
    <!--part_name:PartName22-->
    Description of the next part etc...
    <br>
    <!--part_name:PartName33-->
    Description of the last part etc...
    <br>
    ';
    
    function unComment ($matches){
        $visible = str_replace('<!--', '', $matches[0]);
        $visible = str_replace('-->', '', $visible);
        return $visible;
    }
    
    $newString = preg_replace_callback('#<!--part_name:(.*?)-->#', 'unComment', $oldString);
    
    echo '<p>'.$newString.'</p>';
    

    显示:

    part_name:PartName11 Description of the part etc...
    part_name:PartName22 Description of the next part etc...
    part_name:PartName33 Description of the last part etc...
    

    【讨论】:

      【解决方案2】:

      MyString 示例

      <!--part_name:Part - 1,en, Slow-->
      
      Frame
      
      <!--nextpage--><!--part_name:Part - 2,tr,Fast-->
      
      Frame
      
      <!--nextpage--><!--part_name:Part - 3,en,FHD-->
      
      Frame
      
      <!--nextpage-->
      

      我想在一个数组中获取以下参数。

      第 1 部分,英文,慢

      使用上面的代码,所有参数都列在另一个之下,我无法完全按照我想要的方式获取数据。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-27
        相关资源
        最近更新 更多