English 中文(简体)
如何从扼杀中删除空段落的标签?
原标题:How to remove empty paragraph tags from string?
  • 时间:2010-09-28 01:40:21
  •  标签:
  • php

我与“语言压力”模板存在一个小小 co的问题。 这是在模板中使用的代码一:

<?php echo teaser(40); ?>

在我的职能中,我利用这一手段来打断标签,只从允许的标签中获取内容。

<?php
function teaser($limit) {
    $content = explode(   , get_the_content(), $limit);
    if (count($content)>=$limit) {
    array_pop($content);
    $content = implode(" ",$content). ... ;
    } else {
    $content = implode(" ",$content);
    }   
    $content = preg_replace( /[.+]/ ,  , $content);
    $content = apply_filters( the_content , $content); 
    $content = str_replace( ]]> ,  ]]&gt; , $content);
    $content = strip_tags($content,  <p><a><ul><li><i><em><strong> );
    return $content;
}
?>

问题: 我使用上述代码,从内容中删除标签,但语言压力已经将图像标签放在了段落中。 因此,结果就是一个空洞的 paragraph子,其图像被抹去。

Just for the sake of cleaning up my code and useless empty tags. My question is how to remove empty paragraph tags?

<p></p>

预祝福!

最佳回答

利用这一规定删除空洞段落

/<p[^>]*><\/p[^>]*>/

实例

<?php
$html = "abc<p></p><p>dd</p><b>non-empty</b>"; 
$pattern = "/<p[^>]*><\/p[^>]*>/"; 
//$pattern = "/<[^/>]*>([s]?)*</[^>]*>/";  use this pattern to remove any empty tag

echo preg_replace($pattern,   , $html); 
// output
//abc<p>dd</p><b>non-empty</b>
?>
问题回答

这样就能够排除所有空洞的<代码>p tags,即使它们含有spaces&nbps;内。

$str = "<p>  </p><p> &nbsp; </p><p>&nbsp</p><p>Sample Text</p>";

echo preg_replace("/<p[^>]*>(?:s|&nbsp;)*</p>/",   , $str);

这只是对应的:<条码>和t;p>Sampletext</p>。

这项职能消除了空洞元素,然后是以往被拆除的分子制造的其他空洞元素。 (示例包括空间、表格和运输收益。)

function teaser( $html ) {
    $html = str_replace(  &nbsp; ,    , $html );
    do {
        $tmp = $html;
        $html = preg_replace(
             #<([^ >]+)[^>]*>[[:space:]]*</1># ,   , $html );
    } while ( $html !== $tmp );

    return $html;
}

举以下例子:

<?php

    $html =  
    <p>Hello!
        <div class="foo">
            <p id="nobody">
                <span src="ok">&nbsp;</span>
            </p>
        </div>
    </p>
     ;

echo teaser( $html );

?>

职能回报:

<p>Hello!

</p>

使用体_取代空洞的<代码><p>/p>标签。

$content = str_replace("<p></p>","",$content);

任何更先进的情况都需要定期表达。

$content = preg_replace( /<p[^>]*?></p>/ , $content);

最新情况:

$content = preg_replace( /<p [^>]*></p>/ , $content);




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签