/*
* 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 ( $subs, true );
/*
+--------+
| Output |
+--------+
<span class="bolda">,</span><span class="a">dsdd</span><span class="bolda">,</span>
*/
* 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 ( $subs, true );
/*
+--------+
| Output |
+--------+
<span class="bolda">,</span><span class="a">dsdd</span><span class="bolda">,</span>
*/
1 comments:
great
Post a Comment