[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] [PHP] Gennegå tekst og tilpasse tags



Anders Ossowicki wrote:
> Nogle forslag til hvordan dette problem løses?
Jeg fandt lige dette i mine gemmer, håber det kan bruges:
<?php
function pr_bold( &$text )
{
    $temp = "<b>".substr( $text, 3, strlen($text)-7 )."</b>";
    $text = $temp;
}
function pr_italic( &$text )
{
    $temp = "<i>".substr( $text, 3, strlen($text)-7 )."</i>";
    $text = $temp;
}
function pr_underline( &$text )
{
    $temp = "<u>".substr( $text, 3, strlen($text)-7 )."</u>";
    $text = $temp;
}
function pr_url( &$text )
{
    // Accepts [url]http://www.php.net[/url] or
[url=http://www.php.net]phpSite[/utl]
    if( $text[4] == "=" ) {
        // look for the closing end
        $pos = strpos( $text, "]" );
        if( $pos === false || $pos == strlen($text)-1 ) {
            $text = "**error**";
            return;
        }
        $url = substr( $text, 5, $pos-5 );
        $text = substr( $text, $pos+1, strlen($text)-($pos+2)-5 );
    } else if( $text[4] == "]" ) {
        $text = substr( $text, 5, strlen($text)-11 );
        $url = $text;
    } else {
        $text = "****error****";
        return;
    }
    if( substr( $url, 0, 7 ) != "http://"; ) {
        $text = "****error****";
        return;
    }
    $temp = "<a href=\"$url\">$text</a>";
    $text = $temp;
}

function pr_quote( &$text )
{
    $text = substr( $text, 7, strlen($text)-15 );
    $temp = "<div>".$text."</div>";
    $text = $temp;
}

function pr_code( &$text )
{
    $text = substr( $text, 6, strlen($text)-13 );
    $temp = "<div align=\"left\" style=\"background-color: #FFCC99;
font-family: 'Courier New', Courier, monospace; color: #000099; width: 90%
\">".$text."</div>";
    $text = $temp;
}

function pr_img( &$text )
{
    // Accepts [img]target to img[/img] or [img=Alternate text]target to
img[/img]
    if( $text[4] == "=" ) {
        $pos = strpos( $text, "]" );
        if( $pos === false || $pos == strlen($text)-1 ) {
            $text = "**error**";
            return;
        }
        $alt = substr( $text, 5, $pos-5 );
        $url = substr( $text, $pos+1, strlen($text)-($pos+2)-5 );
    } else if( $text[4] == "]" ) {
        $url = substr( $text, 5, strlen($text)-11 );
        $alt = "";
    } else {
        $text = "****error****";
        return;
    }
    // checar se final é .jpg ou .gif ou .png ?
    $text = "<img src=\"$url\" alt=\"$alt\" border=0>";
}

$processors = array();
$processors["b"]["function"] = "pr_bold";
$processors["b"]["open_tag"] = "[b]";
$processors["b"]["close_tag"] = "[/b]";

$processors["i"]["function"] = "pr_italic";
$processors["i"]["open_tag"] = "[i]";
$processors["i"]["close_tag"] = "[/i]";

$processors["u"]["function"] = "pr_underline";
$processors["u"]["open_tag"] = "[u]";
$processors["u"]["close_tag"] = "[/u]";

$processors["url"]["function"] = "pr_url";
$processors["url"]["open_tag"] = "[url";
$processors["url"]["close_tag"] = "[/url]";

$processors["quote"]["function"] = "pr_quote";
$processors["quote"]["open_tag"] = "[quote]";
$processors["quote"]["close_tag"] = "[/quote]";

$processors["code"]["function"] = "pr_code";
$processors["code"]["open_tag"] = "[code]";
$processors["code"]["close_tag"] = "[/code]";

$processors["img"]["function"] = "pr_img";
$processors["img"]["open_tag"] = "[img";
$processors["img"]["close_tag"] = "[/img]";

function parse_tags3( &$text, &$err )
{
    global $processors;
    $end = false;
    $offset = 0;
        $safe = 0;
    $err = "";
        // pos -> type, level, start, end
    $obj = array();
    $lvl = 0;
        $text = htmlspecialchars($text);
    while( !$end ) {
                if( $safe++ > 1000 ) return -1;
        $pos = strpos( $text, "[", $offset );
        if( $pos === false ) {
            $end = true;
            continue;
        }
        $offset = $pos+1;
        if( $text[$pos+1] != "/" ) {
            $type = "";
            reset( $processors );
            while( $k = each($processors) ) {
                $tag_size = strlen( $k[1]["open_tag"] );
                if( substr( $text, $pos, $tag_size ) == $k[1]["open_tag"] )
{
                    $type = $k[0];
                    break;
                }
            }
            if( $type != "" ) {
                                $obj[] = array( 
                                        "type"  => $type,
                                        "level" => $lvl,
                                        "start" => $pos,
                                        "end"   => -1
                                );
                $lvl++;
            }
        } else {
            $type = "";
            reset( $processors );
            while( $k = each($processors) ) {
                $tag_size = strlen( $k[1]["close_tag"] );
                if( substr( $text, $pos, $tag_size ) == $k[1]["close_tag"] )
{
                    $type = $k[0];
                    break;
                }
            }
            if( $type != "" ) {
                if( $lvl == 0 ) {
                    $err = "<b>error:</b> premature end of tag<br>";
                    return -1;
                }
                $lvl--;
                                $found = false;
                                reset( $obj );
                                while( $k = each($obj) ) {
                                        if( $k[1]["type"] != $type ) continue;
                                        if( $k[1]["end"] != -1 ) continue;
                                        if( $k[1]["level"] != $lvl ) continue;
                                        $obj[$k[0]]["end"] = $pos 
                                                + strlen($processors[$type]["close_tag"]);
                                        $found = true;
                                        break;
                                }
                                if( !$found ) {
                                        $err .= "<b>error:</b> opening tag not found for tag type $type<br>";
                                        return -1;
                                }
                                if( $lvl == 0 ) {
                                $temp = substr( $text, $obj[0]["start"],
                                                $obj[0]["end"] - $obj[0]["start"] );
                                $processors[$obj[0]["type"]]["function"]( $temp );
                                $text = substr_replace( $text, $temp, $obj[0]["start"],
                                                $obj[0]["end"] - $obj[0]["start"] );
                                        $offset = 0;
                                        $lvl = 0;
                                        $obj = array();
                                }
            }
        }
    }
    $text = nl2br($text);
}
?>

-- 
Gert
http://real.men.use.linux.dk


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:44 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *