Return to Snippet

Revision: 3695
at September 2, 2007 08:32 by pitje


Updated Code
<?php

function closetags ( $html )
{
    #put all opened tags into an array
    preg_match_all ( "#<([a-z]+)( .*)?(?!/)>#iU", $html, $result );
    $openedtags = $result[1];

    #put all closed tags into an array
    preg_match_all ( "#</([a-z]+)>#iU", $html, $result );
    $closedtags = $result[1];
    $len_opened = count ( $openedtags );
    # all tags are closed
    if( count ( $closedtags ) == $len_opened )
    {
        return $html;
    }
    $openedtags = array_reverse ( $openedtags );
    # close tags
    for( $i = 0; $i < $len_opened; $i++ )
    {
        if ( !in_array ( $openedtags[$i], $closedtags ) )
        {
            $html .= "</" . $openedtags[$i] . ">";
        }
        else
        {
            unset ( $closedtags[array_search ( $openedtags[$i], $closedtags)] );
        }
    }
    return $html;
}

$str  = "<div>This is some interesting <strong><em>content!</em> And this</strong> line is <em>";
$str .= "abundantly</em> formatted</div>";

$snippet = substr ( $str, 0, 45 );

$snippet = strrpos ( $snippet, "<" ) > strrpos ( $snippet, ">" ) ? rtrim ( substr ( $str, 0, strrpos ( $snippet, "<" ) ) ) . "....." : rtrim ( $snippet ) . ".....";

$x = closetags ( $snippet );

print htmlspecialchars ( $x );

?>

Revision: 3694
at September 2, 2007 08:21 by pitje


Updated Code
<?php
function closetags ( $html )
{
    #put all opened tags into an array
    preg_match_all ( "#<([a-z]+)( .*)?(?!/)>#iU", $html, $result );
    $openedtags = $result[1];

    #put all closed tags into an array
    preg_match_all ( "#</([a-z]+)>#iU", $html, $result );
    $closedtags = $result[1];
    $len_opened = count ( $openedtags );
    # all tags are closed
    if( count ( $closedtags ) == $len_opened )
    {
        return $html;
    }
    $openedtags = array_reverse ( $openedtags );
    # close tags
    for( $i = 0; $i < $len_opened; $i++ )
    {
        if ( !in_array ( $openedtags[$i], $closedtags ) )
        {
            $html .= "</" . $openedtags[$i] . ">";
        }
        else
        {
            unset ( $closedtags[array_search ( $openedtags[$i], $closedtags)] );
        }
    }
    return $html;
}

$str  = "<div>This is some interesting <strong><em>content!</em> And this</strong> line is <em>";
$str .= "abundantly</em> formatted</div>";
$snippet = substr ( $str, 0, 45 );
$snippet = strrpos ( $snippet, "<" ) > strrpos ( $snippet, ">" ) ? rtrim ( substr ( $str, 0, strrpos ( $snippet, "<" ) ) ) . "....." : rtrim ( $snippet ) . ".....";
$x = closetags ( $snippet );

print htmlspecialchars ( $x );
?>

Revision: 3693
at September 2, 2007 08:18 by pitje


Updated Code
<?php
function closetags ( $html )
{
    #put all opened tags into an array
    preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU",$html,$result);
    $openedtags=$result[1];

    #put all closed tags into an array
    preg_match_all("#</([a-z]+)>#iU",$html,$result);
    $closedtags=$result[1];
    $len_opened = count($openedtags);
    # all tags are closed
    if(count($closedtags) == $len_opened){
        return $html;
    }
    $openedtags = array_reverse($openedtags);
    # close tags
    for($i=0;$i<$len_opened;$i++)
    {
        if (!in_array($openedtags[$i],$closedtags))
        {
            $html .= '</'.$openedtags[$i].'>';
        }
        else
        {
            unset($closedtags[array_search($openedtags[$i],$closedtags)]);
        }
    }
    return $html;
}

$str  = "<div>This is some interesting <strong><em>content!</em> And this</strong> line is <em>";
$str .= "abundantly</em> formatted</div>";
$snippet = substr($str,0,45);
$snippet = strrpos($snippet,"<") > strrpos($snippet,">") ? rtrim(substr($str,0,strrpos($snippet,"<"))) . '.....' : rtrim($snippet) . '.....';
$x = closetags($snippet);

print htmlspecialchars($x);
?>

Revision: 3692
at September 2, 2007 08:15 by pitje


Initial Code
<?php
function closetags($html){
#put all opened tags into an array
preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU",$html,$result);
$openedtags=$result[1];

#put all closed tags into an array
preg_match_all("#</([a-z]+)>#iU",$html,$result);
$closedtags=$result[1];
$len_opened = count($openedtags);
# all tags are closed
if(count($closedtags) == $len_opened){
 return $html;
}
$openedtags = array_reverse($openedtags);
# close tags
for($i=0;$i<$len_opened;$i++) {
 if (!in_array($openedtags[$i],$closedtags)){
   $html .= '</'.$openedtags[$i].'>';
 } else {
   unset($closedtags[array_search($openedtags[$i],$closedtags)]);
 }
}
return $html;
}

$str  = "<div>This is some interesting <strong><em>content!</em> And this</strong> line is <em>";
$str .= "abundantly</em> formatted</div>";
$snippet = substr($str,0,45);
$snippet = strrpos($snippet,"<") > strrpos($snippet,">") ? rtrim(substr($str,0,strrpos($snippet,"<"))) . '.....' : rtrim($snippet) . '.....';
$x = closetags($snippet);

print htmlspecialchars($x);
?>

Initial URL

                                

Initial Description
suppose you have some html-formatted text of which you would like to show the first 45 characters.
This function closes any tags that are not-closed because of cutting the first 45 characters.

Note that tags are also counted when defining the first 45 characters!

Initial Title
close tags in a html-snippet

Initial Tags

                                

Initial Language
PHP