Showing posts with label HTML. Show all posts
Showing posts with label HTML. Show all posts

Sunday, November 20, 2011

Convert BBTags to HTML Span Tag (Recursive)

/*
 * Convert BBTags to HTML span tag and put their name into CSS class.
 *
 * @author    Junaid Atari <mj.atari@gmail.com>
 * @version   1.0
 * @param     string   $str              String to process.
 * @param     bool     $tagsLowerCase    Convert the names in lowercase.
 * @param     bool     $removeEmptyTags  Remove the empty tags.
 * @return    string   Processed text
*/

function replaceTagsToClasses 
   $str$tagsLowerCase false$removeEmptyTags true )
{
    if ( !
is_string $str )
         || !
trim $str ) )
             return 
'';
   
    
/* Regex recursive pattern written by Sag-e-Attar Junaid Atari */
    
preg_match_all '/\[([a-z]+)\](.*|(?R))\[\/\1\]/imsU',
                     $str$matches );
   
    if ( !
count $matches[1] ) )
        return 
$str;
   
    
$total count $matches[1] );
   
    
$list array_map (
        
create_function (
            
'$a,$b,$lc,$rmt''return $rmt && !trim($b) ? "" :
            "<span class=\"".($lc ? strtolower($a) : $a)."\">".
            trim($b)."</span>";'
        ),
        
$matches[1], $matches[2],
        array_fill 0$total$tagsLowerCase ),
        
array_fill 0$total$removeEmptyTags )
    );
   
    return 
replaceTagsToClasses (
        
str_replace $matches[0], $list$str ),
        $tagsLowerCase$removeEmptyTags
    
);
}

/*
+---------+
| Example |
+---------+
*/

$subs '[boldA],[/boldA][a][bold][/bold]dsdd[/a][boldA],[/boldA]';
echo 
replaceTagsToClasses $substrue );

/*
+--------+
| Output |
+--------+
<span class="bolda">,</span><span class="a">dsdd</span><span class="bolda">,</span>
*/

Monday, November 14, 2011

Remove Tags from String

/**
 * Remove the tags list from given string.
 *
 * @author     Junaid Atari <mj.atari@gmail.com
>
 * @version    1.1
 * @param      array     $tagsList    Url of page
 * @param      string    $contents    Variable which need to be clean.
 * @return     string    Will direct update the variable in memory.
 */

function removeTags ( array $tagsList$contents )
{
    foreach ( 
$tagsList as $tag )
    {
        
$tag trim preg_quote $tag'/' ) );
        $contents (string) $contents;
         
        if ( 
$tag != '' )   
            
//** Pattern copyright 2011 Junaid Atari
            
return preg_replace '/((<'.$tag.'.*(\/>|>.*<\/'.$tag.
                                  '>)))(\s)?/imsU','', $contents );
        return $contents;
    }