使用PHP将HTML页面转换为纯文本

时间:2015-01-08 18:42:27

标签: php strip-tags

更确切地说,我需要能够像这个脚本那样剥离HTML标签:zubrag.com/tools/html-tags-stripper.php

我需要能够在我的localhost(xampp服务器)上使用任何url执行此操作,但是现在我想使用此url来删除标记,因为它最容易获得:{{3} }

我有什么,不起作用,我不知道为什么或如何解决它。 以下是代码来自:nadeausoftware.com/articles/2007/09/php_tip_how_strip_html_tags_web_page

我已将此行添加到代码中,但它仍无效...

$text = file_get_contents('http://static.anaf.ro/static/10/Timis/Timis.htm');

下面是原始代码(注意原始代码没有上面的那行。那条线是我添加的)

/**
 * Copyright (c) 2008, David R. Nadeau, NadeauSoftware.com.
 * All rights reserved.
 * See:
 *  http://nadeausoftware.com/articles/2007/09/php_tip_how_strip_html_tags_web_page
 */


$text = file_get_contents('http://static.anaf.ro/static/10/Timis/Timis.htm');

function strip_html_tags( $text )
{
    // PHP's strip_tags() function will remove tags, but it
    // doesn't remove scripts, styles, and other unwanted
    // invisible text between tags.  Also, as a prelude to
    // tokenizing the text, we need to insure that when
    // block-level tags (such as <p> or <div>) are removed,
    // neighboring words aren't joined.
    $text = preg_replace(
        array(
            // Remove invisible content
            '@<head[^>]*?>.*?</head>@siu',
            '@<style[^>]*?>.*?</style>@siu',
            '@<script[^>]*?.*?</script>@siu',
            '@<object[^>]*?.*?</object>@siu',
            '@<embed[^>]*?.*?</embed>@siu',
            '@<applet[^>]*?.*?</applet>@siu',
            '@<noframes[^>]*?.*?</noframes>@siu',
            '@<noscript[^>]*?.*?</noscript>@siu',
            '@<noembed[^>]*?.*?</noembed>@siu',

            // Add line breaks before & after blocks
            '@<((br)|(hr))@iu',
            '@</?((address)|(blockquote)|(center)|(del))@iu',
            '@</?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu',
            '@</?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu',
            '@</?((table)|(th)|(td)|(caption))@iu',
            '@</?((form)|(button)|(fieldset)|(legend)|(input))@iu',
            '@</?((label)|(select)|(optgroup)|(option)|(textarea))@iu',
            '@</?((frameset)|(frame)|(iframe))@iu',
        ),
        array(
            ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
            "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0",
            "\n\$0", "\n\$0",
        ),
        $text );

    // Remove all remaining tags and comments and return.
    echo strip_tags( $text );
}

1 个答案:

答案 0 :(得分:1)

它运行正常,但是帖子中链接的正则表达式不起作用。它没有返回正确的字符集,所以试试这个:

function strip_html_tags( $text )
{
    $text = preg_replace(
        array(
          // Remove invisible content
            '@<head[^>]*?>.*?</head>@siu',
            '@<style[^>]*?>.*?</style>@siu',
            '@<script[^>]*?.*?</script>@siu',
            '@<object[^>]*?.*?</object>@siu',
            '@<embed[^>]*?.*?</embed>@siu',
            '@<applet[^>]*?.*?</applet>@siu',
            '@<noframes[^>]*?.*?</noframes>@siu',
            '@<noscript[^>]*?.*?</noscript>@siu',
            '@<noembed[^>]*?.*?</noembed>@siu',
          // Add line breaks before and after blocks
            '@</?((address)|(blockquote)|(center)|(del))@iu',
            '@</?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu',
            '@</?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu',
            '@</?((table)|(th)|(td)|(caption))@iu',
            '@</?((form)|(button)|(fieldset)|(legend)|(input))@iu',
            '@</?((label)|(select)|(optgroup)|(option)|(textarea))@iu',
            '@</?((frameset)|(frame)|(iframe))@iu',
        ),
        array(
            ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
            "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0",
            "\n\$0", "\n\$0",
        ),
        $text );
    return strip_tags( $text );
}

/* Read an HTML file */
$raw_text =  file_get_contents('http://static.anaf.ro/static/10/Timis/Timis.htm');

/* Get the file's character encoding from a <meta> tag */
preg_match("/<meta[^>]+charset=['\"]?(.*?)['\"]?[\/\s>]/i", $raw_text, $matches );
$encoding = $matches[1];

/* Convert to UTF-8 before doing anything else */
$utf8_text = iconv( $encoding, "utf-8", $raw_text );

/* Strip HTML tags and invisible text */
$utf8_text = strip_html_tags( $utf8_text );

/* Decode HTML entities */
$utf8_text = html_entity_decode( $utf8_text, ENT_QUOTES, "UTF-8" );
echo $utf8_text;

我改变了什么:

要获得正确的字符集,我只需更换此

/* Get the file's character encoding from a <meta> tag */
preg_match( '@<meta\s+http-equiv="Content-Type"\s+content="([\w/]+)(;\s+charset=([^\s"]+))?@i', $raw_text, $matches );
$encoding = $matches[3];

用这个

preg_match("/<meta[^>]+charset=['\"]?(.*?)['\"]?[\/\s>]/i", $raw_text, $matches );
$encoding = $matches[1];

编辑1: 猜猜网站上的脚本在从您提供的URL中剥离标记时遇到了一些问题。它显示了很多。我想剥离标签的最好方法就是在开口之间剥离所有东西。和第一次结束&gt;。但我目前对正则表达式一无所知,也许谷歌可以提供帮助:)