php - How do I surround all text pieces with paragraph tags? -


i want put paragraph tags around text items. should therefore avoid tables , other elements. how do that? guess somehow can made preg_replace?

here couple of functions should want do:

// nl2p // function convert newlines html paragraphs // without paying attention html tags. feed raw string , // return string sectioned html paragraphs  function nl2p($str) {     $arr=explode("\n",$str);     $out='';      for($i=0;$i<count($arr);$i++) {         if(strlen(trim($arr[$i]))>0)             $out.='<p>'.trim($arr[$i]).'</p>';     }     return $out; }    // nl2p_html // function add paragraph tags around textual content of html file, leaving // html intact // function assumes html syntax correct , '<' , '>' characters // not used in of values tag attributes. if these assumptions not met, // mass paragraph chaos may ensue. safe.  function nl2p_html($str) {      // if find end of html header, assume part of standard html file. cut off including     // end of head , save in our output string, trim head off of input. because don't     // want surrount html title tag or style or script code in paragraph tags.      if(strpos($str,'</head>')!==false) {         $out=substr($str,0,strpos($str,'</head>')+7);         $str=substr($str,strpos($str,'</head>')+7);     }      // first, explode input string based on wherever find html tags, start '<'     $arr=explode('<',$str);      // next, loop through array broken html tags , textual content, or     // after >     for($i=0;$i<count($arr);$i++) {         if(strlen(trim($arr[$i]))>0) {             // add '<' on since became collateral damage in our explosion rest of tag             $html='<'.substr($arr[$i],0,strpos($arr[$i],'>')+1);              // take portion of string after end of tag , explode newline. since after             // end of html tag, must textual content.             $sub_arr=explode("\n",substr($arr[$i],strpos($arr[$i],'>')+1));              // initialize output string next loop             $paragraph_text='';              // loop through new array , add paragraph tags (<p>...</p>) around element isn't empty             for($j=0;$j<count($sub_arr);$j++) {                 if(strlen(trim($sub_arr[$j]))>0)                     $paragraph_text.='<p>'.trim($sub_arr[$j]).'</p>';             }              // put text onto end of html tag , put in our output string             $out.=$html.$paragraph_text;         }      }      // throw our program     return $out; } 

the first of these, nl2p(), takes string input , converts array wherever there newline ("\n") character. goes through each element , if finds 1 isn't empty, wrap <p></p> tags around , add string, returned @ end of function.

the second, nl2p_html(), more complicated version of former. pass html file's contents string , wrap <p> , </p> tags around non-html text. exploding string array delimiter < character, start of html tag. then, iterating through each of these elements, code end of html tag , take comes after new string. new string exploded array delimiter newline ("\n"). looping through new array, code looks elements not empty. when finds data, wrap in paragraph tags , add output string. when loop finished, string added onto html code , amended output buffer string returned once function has completed.

tl;dr: nl2p() convert string html paragraphs without leaving empty paragraphs , nl2p_html() wrap paragraph tags around contents of body of html document.

i tested on couple of small example html files make sure spacing , other things don't ruin output. code that's generated nl2p_html() may not w3c-compliant, either, wrap anchors around paragraphs , rather other way around.

hope helps.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -