Zend - The PHP Company




HTML

Add Code


HTML Wrapper for CSS styles  

Type: code fragment
Added by: tswan
Entered: 02/03/2000
Last modified: 08/12/1999
Rating: **** (7 votes)
Views: 8822

If you write the attributes for your text in CSS this will wrap and set the Translates CSS attributes into HTML3 tags while keeping the CSS style as inline. Also, allows for setting a default style for the page.

Supported attributes FACE, COLOR, SIZE, BOLD, ITALIC, UNDERLINE.



<?PHP
global $base_style_begin;
global 
$base_style_end;

    function 
style($text$style "base"$extras false) {
    
/*
    Description
        Converts CSS, $style, to traditional HTML tags
        <B>,<I>,<U>,<FONT FACE,<FONT COLOR,<FONT SIZE
        font-family : '{font name}',...
        font-size : {size in points ??pt, or size in pixels ??px}
        font-style : [italic];
        text-decoration : [underline];
        font-weight : [bold];
    Author
        Thomas J. Swan <tswan@olemiss.edu>
    Parameters
        $text:    The string to be altered
        $style:    Style (CSS) of text
        $use_base_font:    Unused right now... 1.2
    Revision History
        1.0    2/25/2000    Creation of function
        1.1    2/29/2000    Add the parsin' of bold, face, style, decoration and color attributes
        1.2    3/02/2000    Modified size determination to reduces number of ifs... (BSearch)
        1.3    3/09/2000  Added <P> and <A> tag support
    Example
        The segment
            echo style("Test Me","font-size:12pt;font-family : 'Arial','Helvetica'; font-weight : bold; font-style: italic; text-decoration: underline;");
        Results in
            <U><I><B><FONT SIZE="3" FACE="'Arial','Helvetica'"  STYLE="font-size:12pt;font-family : 'Arial','Helvetica'; font-weight : bold; font-style: italic; text-decoration: underline;">TEST</FONT></B></I></U>
*/
        
global $base_style_begin;
        global 
$base_style_end;

        if (
$style != "base") {
            
$font_tag_attributes false;

            if (
$start strstr($style,"color")) {
                
$fam_begin strpos($start,":") + 1;
                if (!(
$fam_end strpos($start,";"$fam_begin))) {
                    
$fam_end strlen($start);
                }

                
$font_tag_attributes .= "COLOR="".trim(substr($start$fam_begin$fam_end - $fam_begin))."" ";
            }

            if (
$start strstr($style,"size")) {
                
$fam_begin strpos($start,":") + 1;
                if (!(
$fam_end strpos($start,";"$fam_begin))) {
                    
$fam_end strlen($start);
                }

                
$size trim(substr($start$fam_begin$fam_end $fam_begin));
                
/* This should be a number 'kind of' */

                
if ($size 17)
                    if (
$size 23)
                        if (
$size 35)
                            
$size 7;
                        else
                            
$size 6;
                    else
                        
$size 5;
                else if (
$size 13)
                    if (
$size 9)
                        if (
$size 11)
                            
$size 2;
                        else
                            
$size 1;
                    else
                        
$size 3;
                else
                    
$size 4;
                
$font_tag_attributes .= "SIZE="$size" ";
            }

            if (
$start strstr($style,"family")) {
                
$fam_begin strpos($start,":") + 1;
                if (!(
$fam_end strpos($start,";"$fam_begin))) {
                    
$fam_end strlen($start);
                }
                
$font_tag_attributes .= "FACE="".trim(substr($start$fam_begin$fam_end - $fam_begin))."" ";
            }

            
$base "<FONT $font_tag_attributes STYLE="$style">$text</FONT>";

            if (
strpos($style,"bold")) $base "<B>" $base "</B>";
            if (
strpos($style,"italic")) $base "<I>" $base "</I>";
            if (
strpos($style,"underline")) $base "<U>" $base "</U>";

            return 
$base;
        } else {
            return 
$base_style_begin $text $base_style_end;
        }
    }

    function 
set_style($style false) {
    
/*
    Description
        Sets the default style for the text using global vars $base_style_begin and 
        $base_style_end.  For use in conjunction style($text, $style="null")
    Author
        Thomas J. Swan <tswan@olemiss.edu>
    Parameters
        $text:    The string to be altered
        $style:    Style (CSS) of text
    Revision History
        1.0    2/26/2000    Creation of Function
        1.1    2/29/2000    Add the parsin' of bold, face, style, decoration and color attributes
        1.2    3/02/2000    Modified size determination to reduces number of ifs... (BSearch)
        1.3    3/09/2000  Added <P> and <A> tag support
    Example
        set_style("font-family:'Arial','Helvetica';font-color:green;font-size:12pt");
        echo style("Test Me");
    Results in
            <FONT FACE="'Arial','Helvetica'" SIZE="3" COLOR="green" STYLE="font-family:'Arial','Helvetica';fon-color:green;font-size:12pt">Bob</FONT>    
*/
        
global $base_style_begin;
        global 
$base_style_end;

        if (
$style) {
            if (
$start strstr($style,"size")) {
                
$fam_begin strpos($start,":") + 1;
                if (!(
$fam_end strpos($start,";"$fam_begin))) {
                    
$fam_end strlen($start);
                }
    
                
$size trim(substr($start$fam_begin$fam_end $fam_begin));
                
/* This should be a number 'kind of' */
    
                
if ($size 17)
                    if (
$size 23)
                        if (
$size 35)
                            
$size 7;
                        else
                            
$size 6;
                    else
                        
$size 5;
                else if (
$size 13)
                    if (
$size 9)
                        if (
$size 11)
                            
$size 2;
                        else
                            
$size 1;
                    else
                        
$size 3;
                else
                    
$size 4;
    
                
$font_tag_attributes .= "SIZE="$size" ";
            }

            if (
$start strstr($style,"family")) {
                
$fam_begin strpos($start,":") + 1;
                if (!(
$fam_end strpos($start,";"$fam_begin))) {
                    
$fam_end strlen($start);
                }
                
$font_tag_attributes .= "FACE="".trim(substr($start$fam_begin$fam_end - $fam_begin))."" ";
            }

            
$base_style_begin "<FONT $font_tag_attributes STYLE="$style">";
            
$base_style_end "</FONT>";
            if (
strpos($style,"bold"))
                
$base_style_begin "<B>" $base_style_begin;
                
$base_style_end $base_style_end "</B>";
            if (
strpos($style,"italic"))
                
$base_style_begin "<I>".$base_style_begin;
                
$base_style_end $base_style_end "</I>";
            if (
strpos($style,"underline"))
                
$base_style_begin "<U>".$base_style_begin;
                
$base_style_end $base_style_end "</U>";
            return;
        }
    }

    function 
astyle($href$text$style "base"$extras false) {
    
/*
    Description
        returns an ANCHOR tag with CSS formatting
        
        Converts CSS, $style, to traditional HTML tags
        <B>,<I>,<U>,<FONT FACE,<FONT COLOR,<FONT SIZE
        font-family : '{font name}',...
        font-size : {size in points ??pt, or size in pixels ??px}
        font-style : [italic];
        text-decoration : [underline];
        font-weight : [bold];
    Author
        Thomas J. Swan <tswan@olemiss.edu>
    Parameters
        $text:    The string to be altered
        $style:    Style (CSS) of text
        $use_base_font:    Unused right now... 1.2
    Revision History
        1.0    2/25/2000    Creation of function
        1.1    2/29/2000    Add the parsin' of bold, face, style, decoration and color attributes
        1.2    3/02/2000    Modified size determination to reduces number of ifs... (BSearch)
        1.3    3/09/2000  Added <P> and <A> tag support
    Example
        The segment
            echo style("Test Me","font-size:12pt;font-family : 'Arial','Helvetica'; font-weight : bold; font-style: italic; text-decoration: underline;");
        Results in
            <U><I><B><FONT SIZE="3" FACE="'Arial','Helvetica'"  STYLE="font-size:12pt;font-family : 'Arial','Helvetica'; font-weight : bold; font-style: italic; text-decoration: underline;">TEST</FONT></B></I></U>
*/
        
global $base_style_begin;
        global 
$base_style_end;

        if (
$style != "base") {
            
$font_tag_attributes false;

            if (
$start strstr($style,"color")) {
                
$fam_begin strpos($start,":") + 1;
                if (!(
$fam_end strpos($start,";"$fam_begin))) {
                    
$fam_end strlen($start);
                }

                
$font_tag_attributes .= "COLOR="".trim(substr($start$fam_begin$fam_end - $fam_begin))."" ";
            }

            if (
$start strstr($style,"size")) {
                
$fam_begin strpos($start,":") + 1;
                if (!(
$fam_end strpos($start,";"$fam_begin))) {
                    
$fam_end strlen($start);
                }

                
$size trim(substr($start$fam_begin$fam_end $fam_begin));
                
/* This should be a number 'kind of' */

                
if ($size 17)
                    if (
$size 23)
                        if (
$size 35)
                            
$size 7;
                        else
                            
$size 6;
                    else
                        
$size 5;
                else if (
$size 13)
                    if (
$size 9)
                        if (
$size 11)
                            
$size 2;
                        else
                            
$size 1;
                    else
                        
$size 3;
                else
                    
$size 4;
                
$font_tag_attributes .= "SIZE="$size" ";
            }

            if (
$start strstr($style,"family")) {
                
$fam_begin strpos($start,":") + 1;
                if (!(
$fam_end strpos($start,";"$fam_begin))) {
                    
$fam_end strlen($start);
                }
                
$font_tag_attributes .= "FACE="".trim(substr($start$fam_begin$fam_end - $fam_begin))."" ";
            }

            
$base "<A HREF="$href" STYLE="$style"><FONT $font_tag_attributes STYLE="$style">$text</FONT></A>";

            if (
strpos($style,"bold")) $base "<B>" $base "</B>";
            if (
strpos($style,"italic")) $base "<I>" $base "</I>";
            if (
strpos($style,"underline")) $base "<U>" $base "</U>";

            return 
$base;
        } else {
            return 
$base_style_begin $text $base_style_end;
        }
    }

    function 
pstyle($text$style "base"$extras false) {
    
/*
    Description
        returns an ANCHOR tag with CSS formatting
        
        Converts CSS, $style, to traditional HTML tags
        <B>,<I>,<U>,<FONT FACE,<FONT COLOR,<FONT SIZE
        font-family : '{font name}',...
        font-size : {size in points ??pt, or size in pixels ??px}
        font-style : [italic];
        text-decoration : [underline];
        font-weight : [bold];
    Author
        Thomas J. Swan <tswan@olemiss.edu>
    Parameters
        $text:    The string to be altered
        $style:    Style (CSS) of text
        $use_base_font:    Unused right now... 1.2
    Revision History
        1.0    2/25/2000    Creation of function
        1.1    2/29/2000    Add the parsin' of bold, face, style, decoration and color attributes
        1.2    3/02/2000    Modified size determination to reduces number of ifs... (BSearch)
        1.3    3/09/2000  Added <P> and <A> tag support
    Example
        The segment
            echo style("Test Me","font-size:12pt;font-family : 'Arial','Helvetica'; font-weight : bold; font-style: italic; text-decoration: underline;");
        Results in
            <U><I><B><FONT SIZE="3" FACE="'Arial','Helvetica'"  STYLE="font-size:12pt;font-family : 'Arial','Helvetica'; font-weight : bold; font-style: italic; text-decoration: underline;">TEST</FONT></B></I></U>
*/
        
global $base_style_begin;
        global 
$base_style_end;

        if (
$style != "base") {
            
$font_tag_attributes false;

            if (
$start strstr($style,"color")) {
                
$fam_begin strpos($start,":") + 1;
                if (!(
$fam_end strpos($start,";"$fam_begin))) {
                    
$fam_end strlen($start);
                }

                
$font_tag_attributes .= "COLOR="".trim(substr($start$fam_begin$fam_end - $fam_begin))."" ";
            }

            if (
$start strstr($style,"size")) {
                
$fam_begin strpos($start,":") + 1;
                if (!(
$fam_end strpos($start,";"$fam_begin))) {
                    
$fam_end strlen($start);
                }

                
$size trim(substr($start$fam_begin$fam_end $fam_begin));
                
/* This should be a number 'kind of' */

                
if ($size 17)
                    if (
$size 23)
                        if (
$size 35)
                            
$size 7;
                        else
                            
$size 6;
                    else
                        
$size 5;
                else if (
$size 13)
                    if (
$size 9)
                        if (
$size 11)
                            
$size 2;
                        else
                            
$size 1;
                    else
                        
$size 3;
                else
                    
$size 4;
                
$font_tag_attributes .= "SIZE="$size" ";
            }

            if (
$start strstr($style,"family")) {
                
$fam_begin strpos($start,":") + 1;
                if (!(
$fam_end strpos($start,";"$fam_begin))) {
                    
$fam_end strlen($start);
                }
                
$font_tag_attributes .= "FACE="".trim(substr($start$fam_begin$fam_end - $fam_begin))."" ";
            }

            
$base "<P STYLE="$style"><FONT $font_tag_attributes STYLE="$style">$text</FONT></P>";

            if (
strpos($style,"bold")) $base "<B>" $base "</B>";
            if (
strpos($style,"italic")) $base "<I>" $base "</I>";
            if (
strpos($style,"underline")) $base "<U>" $base "</U>";

            return 
$base;
        } else {
            return 
$base_style_begin $text $base_style_end;
        }
    }


?>


Usage Example


Examples located in Source


Rate This Script





Search



This Category All Categories