* @version 0.2
* @copyright Andreas Jaggi 2004
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @package x-log
* @param string Input String
* @param array Acronyms with Definitons
* @param boolean Decently Mode
* @param array Decently Chars
* @return string Output String
*/
function acronymize ( $input, $acronyms, $acronymtag = 'acronym', $decently = false, $decentlyChars = false ) {
/* do the replacing for every acronym */
foreach ( $acronyms as $acronym=>$attributes ) {
/* build the replacing string */
$description = '<'.$acronymtag;
foreach( $attributes as $k=>$v ) {
$description .= ' '.$k.'="'.$v.'"';
}
$description .= '>'.$acronym.''.$acronymtag.'>';
/* reset positons */
$tag = false;
$attribute = false;
$inacronym = 0;
/* this checks char by char */
for ( $i = 0 ; $i < strlen($input) ; $i++ ) {
/* get the char */
$char = substr($input, $i, 1);
if ( !$tag && $attribute === false && $char == '<' ) {
/* we arent inside of a tag and we arent inside of an attribute,
* so lets join the tag */
$tag = true;
/* if the tag is an ending acronym,
* decrease the acronym-level */
if(substr($input, $i+1, strlen($acronymtag)+1) == '/'.$acronymtag ) {
$inacronym--;
}
/* if the tag is a starting acronym,
* increase the acronym-level */
if(substr($input, $i+1, strlen($acronymtag)) == $acronymtag ) {
$inacronym++;
}
}
if ( $tag ) {
/* we are inside of a tag */
if ( $attribute === false ) {
/* we arent inside of an attribute (e.g. inside "" or '')
* so its possible to leave the tag or to join an attribute */
switch ( $char ) {
case '>':
/* leave the tag */
$tag = false;
break;
case '"':
case '\'':
/* join a new attribute */
$attribute = $char;
break;
}
}else{
/* we are inside of an attribute, so its not possible to leave the tag
* but its possible to leave the attribute */
if ( $char == $attribute ) {
/* leave the attribute */
$attribute = false;
}
}
}
if ( $inacronym == 0 && !$tag && $char == substr($acronym, 0, 1) ) {
/* we arent inside of an acronym and we arent inside of a tag,
* and the char is equal to the first char of the acronym */
if( substr($acronym, 0) == substr($input, $i, strlen($acronym)) ) {
/* the next chars are equal to the acronym,
* lets see if we can replace them */
if ( $decently ) {
/* we are in decently mode */
if ( $i == 0 || in_array(substr($input, $i-1, 1), $decentlyChars) ) {
/* the char before the acronym is in decentlyChars */
if ( strlen($input) == $i+strlen($acronym) || in_array(substr($input, $i+strlen($acronym), 1), $decentlyChars) ) {
/* the char after the acronym is in decentlyChars,
* so we can replace the acronym */
$input = substr($input, 0, $i).$description.substr($input, $i+strlen($acronym));
$i += strlen($description);
}
}
}else{
/* we arent in decently mode,
* so we can replace the acronym */
$input = substr($input, 0, $i).$description.substr($input, $i+strlen($acronym));
$i += strlen($description);
}
}
}
}
}
return $input;
}
/* an example */
$acronyms = array(
'PHP' => array(
'title' => 'PHP Hypertext Preprocessor',
'lang' => 'en'
),
'XML' => array(
'title' => 'Extensible Markup Language'
)
);
$input = 'XML Foo PHP
PHP PHP & XMLPHPXML XML'."\n".'BAR PHP-XML';
$decentlyChars = array (
'-',
' ',
'.',
'_',
',',
';',
':',
'?',
'\'',
'"',
'(',
')',
'[',
']',
'{',
'}',
'+',
'!',
'=',
'/',
'\\',
'&',
'%',
'*',
'<',
'>',
'$',
"\n"
);
echo acronymize( $input, $acronyms, 'acronym', true, $decentlyChars );
?>