正则表达式来替换多行生成的标签

时间:2018-08-24 10:30:50

标签: javascript php

我正在尝试为我创建一个实用程序,该实用程序可以将Word文档转换为干净的HTML,但停留在多行生成的清理标签上。如果有人想在线查看该工具,但我不希望此帖子被标记为垃圾邮件,我可以共享该链接。

enter image description here

我想使用php或javascript用一个标签替换

..

...

我使用过HTML Tidy。

function cleaning($string, $tidyConfig = null) 
{
    $out = array ();
    $config = array (
            'indent' => true,
            'show-body-only' => false,
            'clean' => true,
            'output-xhtml' => true,
            'preserve-entities' => true 
    );
    if ($tidyConfig == null) {
        $tidyConfig = &$config;
    }
    $tidy = new tidy ();
    $out ['full'] = $tidy->repairString ( $string, $tidyConfig, 'UTF8' );
    unset ( $tidy );
    unset ( $tidyConfig );
    $out ['body'] = preg_replace ( "/.*<body[^>]*>|<\/body>.*/si", "", $out ['full'] );
    $out ['style'] = '<style type="text/css">' . preg_replace ( "/.*<style[^>]*>|<\/style>.*/si", "", $out ['full'] ) . '</style>';
    return ($out);
}

1 个答案:

答案 0 :(得分:1)

对于我来说,根据您的示例代码,您所期望的结果到底是什么也不十分清楚。

例如,您将以下内容准确地转换为什么,以及为什么将其转换为有效的HTML代码?

select (case when country in ('India','Portugal') then country else 'others' end) as country,
       count(distinct customer_id) as customers
from table t
group by (case when country in ('India','Portugal') then country else 'others' end)

您可以使用<p> <strong>Minify</strong>&nbsp;<strong>CSSis all free</strong> </p> <p> strip_tags()转换为以下示例:

strip_tags($text,'<p>')

或使用<p> Minify&nbsp;CSSis all free </p> <p>

strip_tags($text,'<strong>')

<strong>Minify</strong>&nbsp;<strong>CSSis all free</strong> trim()的组合可以将其清理成单行,例如:

str_replace()

强烈建议您更新您的问题,以确保清楚和最终结果。