从 PHP 中的 XML 字符串中删除 <br /> 标签

Removing the <br /> tags from an XML string in PHP

我有一个上传表单,允许用户提交 XML 文件,然后将其作为新 post 的内容插入 WordPress。这工作正常,但是 XML 字符串在我想删除的每一行之后都有一个 </br> 标记。我查看了 strip_tags,但允许使用的标签太多了。

如何在导入内容时从字符串中删除这些 br 元素?

function slicer_profile_submit()
{
    // if the submit button is clicked, submit
    if (isset($_POST['slicer-profile-submitted']))
    {
        $xml = simplexml_load_file($_FILES['slicer-profile']['tmp_name']) or die("Error: Cannot upload file. Please contact the administrator.");
        $contents = '<textarea rows="12">' . $xml->asXML() . '</textarea>';

        // sanitize form values
        $profile_author = sanitize_text_field( $_POST["slicer-profile-author"] );
        $profile_name = sanitize_text_field( $_POST["slicer-profile-name"] );
        $profile_model = $_POST["slicer-profile-model"];
        $profile_software = $_POST["slicer-profile-software"];

        // Create post object
        $slicer_profile = array(
            'post_title'    => $profile_name,
            'post_content'  => $contents,
            'post_type' => 'slicer_profiles',
            'post_status'   => 'publish',
            'post_author'   => 1,
            'meta_input' => array(
                'slicer_profile_author' => $profile_author
            )
        );

        // Insert the post into the database
        wp_insert_post( $slicer_profile );
    }
}

结果:

使用 preg_replace 从您的回复中删除不需要的字符或字符串。

它将清除您在函数中提到的字符串,并将return输出

$properresponse = preg_replace('/&nbsp;|<br \/>/i', '', $your_string);