自定义标签替换函数示例
function custom_markup_translate($str){
//array with regular expressions that match custom tags
$search = array(
"#\{bold\}(.*?)\{/bold\}#is",
"#\{italic\}(.*?)\{/italic\}#is",
"#\{underline\}(.*?)\{/underline\}#is",
"#\{heading\}(.*?)\{/heading\}#is",
"#\{subheading\}(.*?)\{/subheading\}#is",
"#\{link:(.*?)\}(.*?)\{/link\}#is",
"#\{elink:(.*?)\}(.*?)\{/elink\}#is",
"#\{unordered-list\}(.*?)\{/unordered-list\}\s*#is",
"#\{ordered-list\}(.*?)\{/ordered-list\}\s*#is",
"#\\s*\{list-element}(.*?)\{/list-element\}\s*#is",
"#\{picture:(.*?)\}#is",
"#\t#is",
"#\{comment}(.*?)\{/comment\}#is"
);
//array with HTML replacements
$replace = array(
'\\1',
'\\1',
'\\1',
'
\\1
','
\\1
','
- \\1
'
- \\1
'
\\1','',
'',
''
);
//perform the replacement
$step_1 = preg_replace($search, $replace, $str);
$step_2 = preg_split('#(\{HTML\}.*?\{HTML\})#is', $step_1, -1, PREG_SPLIT_DELIM_CAPTURE);
$return = '';
foreach($step_2 as $s2){
if(preg_match('#\{HTML\}#', $s2)){
$return .= preg_replace('#\{/?HTML\}#is', '', $s2);
}else{
$return .= nl2br($s2);
}
}
//return HTML markup
return $return;
}
?>
测试文本
{HEADING}Using a Custom Markup Language to Generate Optimized HTMl{/HEADING}
As we mentioned earlier, using a WYSIWYG editor frequently presents a problem
with regard to on-page optimization. Frequently, the editors do bot generate HTML
that uses tags that adequately delineate the stauctural meaning of elements in a
page. Since heading tags, such as h1, ul, and strong are indicators of the structure
within adocument, not using them will probably {BOLD}{ITALIC}decrease{/ITALIC}{/BOLD}
the rankings of a page, especially when a search engine is relying on on-page factors.