XHTML2-to-HTML
Item 9 of 9
1 <?php 2 3 //====================================================================================================== 4 // title: XHTML2 to HTML (v0.5) 5 // author: Daniel Pupius (pupius.co.uk) 6 // created: 16/06/2003 7 // description: PHP function to convert XHTML2 to HTML. Only converts the main difference, for example 8 // <object> of type image gets converted to <img>; <l></l> gets turned into <br />; 9 // <blockcode> becomes <pre>; and of course <section> gets stripped out and the <h> tags 10 // get numbered accordingly. Most other features of XHTML2 can be mapped directly to 11 // HTML. 12 // 13 // NOTE: as of 16/06/2003 this has not been extensivly used. There may be rendering 14 // issues in certain situations. Check back for later versions. 15 // 16 // To use simply call the function on a string: 17 // $html = xhtml2_to_html($xhtml2); 18 //====================================================================================================== 19 20 function xhtml2_to_html($xhtml2) { 21 $html = $xhtml2; 22 23 //change blockcode to pre 24 $html = preg_replace("/\<(\/)?blockcode\>/i","<\\1pre>",$html); 25 26 //turn objects of type image/gif, image/jpg and image/png into images 27 $html = preg_replace("/\<object(.*)type\=\"image\/(gif|jpg|jpeg|png)\"(.*)\>(.*)\<\/object\>/ism","<img\\1\\3 />",$html); 28 29 //strip out <l> and replace </l> with <br /> 30 $html = preg_replace("/\<l\>/i","",$html); 31 $html = preg_replace("/\<\/l\>/i","<br />",$html); 32 33 34 //sort out the headings 35 $cursor = 0; 36 $level = 1; //start at <h1>, so a <h> found in the first <section> would be a <h2> 37 $processed = ""; 38 while($cursor<strlen($html)) { 39 $c = substr($html,$cursor,1); 40 41 if(substr($html,$cursor,9) == "<section>") { $level++; $cursor+=8; } 42 else if(substr($html,$cursor,10) == "</section>") { $level — ; $cursor+=9; } 43 else if(substr($html,$cursor,3) == "<h>") { $processed .= "<h$level>"; $cursor+=2; } 44 else if(substr($html,$cursor,4) == "</h>") { $processed .= "</h$level>"; $cursor+=3; } 45 else $processed .= $c; 46 47 $cursor++; 48 } 49 50 $html = $processed; 51 52 $html = preg_replace("/\<(p|pre|h[0-9])/i","\n<\\1",$html); 53 $html = preg_replace("/\<\/(p|pre|h[0-9])>/i","</\\1>\n",$html); 54 55 return $html; 56 } 57 58 ?> 59