либаЛиба,используемая в примере: <? /* * (c) 2001 Alexey Kulentsov, Otamedia Ltd. * * History: * 29.03.2001 - Started * 09.04.2001 - pop('tagname') added * 16.03.2002 - cdata now is conditional */ if(!defined('_CLASS_TAG')){ .define('_CLASS_TAG',1);
// Not completed now, only for testing purposes $body_tags=array('DIV','P','H1','H2','H3','H4','H5','H6','TABLE','FORM','SCRIPT','CENTER','FONT','B','I','U','NOBR','PRE','STRONG','ADDRESS','CODE');
$HTML_dtd=array(
'HTML' =>array('HEAD','BODY','FRAMESET'), 'FRAMESET'=>array('FRAME','FRAMESET'), 'HEAD' =>array('TITLE','SCRIPT'), 'BODY' =>&$body_tags, 'TABLE' =>array('TR','TC'), 'TR' =>array('TD'), 'DIV' =>&$body_tags, 'CENTER' =>&$body_tags, 'TD' =>&$body_tags );
class tag { .var.$tags=array(); .var.$CheckIt=false;
.function tag() .{ ..$arg_list=func_get_args(); ..if(count($arg_list)) ...$this->push($arg_list); .}
.function tagname($tag) .{ ..$tagname=explode(' ',$tag); ..return $tagname[0]; .}
.function push_one($tag) .{ ..$tagname=$this->tagname($tag); ..if($this->CheckIt && count($this->tags)) ..{ global $HTML_dtd; ...$prev=strtoupper($this->tags[count($this->tags)-1]); ...$allowed=&$HTML_dtd[$prev]; ...if(count($allowed)) ....if(!in_array(strtoupper($tagname),$allowed)) .....trigger_error("DTD error: $tag not allowed in $prev",E_USER_WARNING); ..} ..array_push($this->tags,$tagname); ..echo "<$tag>"; .}
.function push() .{.$arg_list=func_get_args(); ..foreach($arg_list as $arg) ...if(gettype($arg)=='array') ....foreach($arg as $item) .....$this->push_one($item); ...else.$this->push_one($arg);.. .}
.function pop_one($ptag=null) .{ ..if(!count($this->tags)) trigger_error('Can\'t pop tag!',E_USER_WARNING); ..$tag=array_pop($this->tags); ..if(isset($ptag) && $tag!=$ptag) ..{.trigger_error("Popped <B></$ptag></B> instead <B></$tag></B>",E_USER_WARNING); ...return false; ..} ..echo "</$tag>"; ..return true; .} .function pop() .{.$arg_list=func_get_args(); ..if(!count($arg_list)) ...$arg_list=array(1); ..foreach($arg_list as $arg) ...switch(gettype($arg)){ ...case 'array': ....foreach($arg as $item) .....$this->pop($item); ....break; ...case 'string':. ....if($arg!='all') ....{.$this->pop_one($arg); .....break; ....} ....$arg=count($this->tags); ...case 'integer': ....while($arg-- > 0) .....$this->pop_one(); ....break; ...default: ....trigger_error('Incorrect argument type in tag->pop()',E_USER_ERROR); ...} .}
}
function SPAN($tag,$text) { $tags=new tag($tag); .echo $text; .$tags->pop('all'); }
function t($tag,$text) {.$tags=new tag(); .return '<'.$tag.'>'.$text.'</'.$tags->tagname($tag).'>'; } function cdata($text,$conditional=false) {. .if($conditional && preg_match('/^[^<>&\'"]*$/',$text)) ..return $text;
.$text=str_replace("]]>","]]>]]<![CDATA[>",$text); .return '<![CDATA['.$text.']]>'; } function to_tag($text) {. .$from=" \"\t\\/:#$%^&*()=+-!`'~?<>[],."; .$retval=strtr($text,$from,str_repeat('_',strlen($from))); .//echo "\r\n<BR>[$text] >> [$retval]"; .return $retval; }
function mark_links($s) { .$s=ereg_replace("(([hH][tT][tT][pP][sS]{0,1}|[fF][tT][pP])://[a-zA-Z0-9\.=?+%~#:&/_-]+)","]]><link><href>\\1</href><text>\\1</text></link><![CDATA[",$s); .$s=ereg_replace("([ ]|^)(www\.[a-zA-Z0-9\.=?+%~#:&/_-]+)( |$)","\\1]]><link><href>http://\\2</href><text>\\2</text></link><![CDATA[\\3",$s); .$s='<![CDATA['.ereg_replace("([_a-zA-Z0-9\.-]+\@[-_a-zA-Z0-9\.]+)","]]><mail_link><href>\\1</href><text>\\1</text></mail_link><![CDATA[",$s).']]>'; .return $s; }
}
?> |