自定义标签替换函数(php)

自定义标签替换函数示例

<?php 
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(
		'<b>\\1</b>',
		'<i>\\1</i>',
		'<u>\\1</u>',
		'<h1 class="some_class">\\1</h1>',
		'<h2 class="some_other_class">\\1</h2>',
		'<a href="\\1">\\2</i>',
		'<a href="\\1" target="_blank">\\2</b>',
		'<ul>\\1</ul>',
		'<ol>\\1</ol>',
		'<li>\\1</li>',
		'<img src="\\1" border="0">',
		'',
		''
		);
	
	//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.

你可能感兴趣的:(自定义标签替换函数(php))