taith Posted June 7, 2007 Share Posted June 7, 2007 just trying to make a flatfile autoformatter... and it works... sorta... except that it splits up like tags... as <td>text</td> becomes <td>text </td> function cleanup($array){ $out=array(); foreach($array as $k=>$v){ if(!empty($v)) $out[$k]=$v; } return $out; } function autoformat($string,$start=0){ $string=cleanup(explode('<',$string)); foreach($string as $k=>$v){ if($string[$k]{0}!='<')$string[$k]='<'.$string[$k]; if($string[$k]{1}=='/') $start--; else $start++; for($i=0; $i<$start; $i++) $string[$k]=' '.$string[$k]; $string[$k].="\n"; } return implode('',$string); } $string='<table><tr><td></td></tr></table>'; echo autoformat($string); Quote Link to comment https://forums.phpfreaks.com/topic/54605-flatfile-autoformater/ Share on other sites More sharing options...
trq Posted June 7, 2007 Share Posted June 7, 2007 Ever thought of using the tidy extension? PS; Indexing a string via $string{3} is being depericated on favour of $string[3]. Quote Link to comment https://forums.phpfreaks.com/topic/54605-flatfile-autoformater/#findComment-270013 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 $string[$k].="\n"; That is where the issue lies. you gotta figure out how to determine which needs to have \n after and which doesn't, which will probably need a manual array of known tags to not add the \n too. Quote Link to comment https://forums.phpfreaks.com/topic/54605-flatfile-autoformater/#findComment-270014 Share on other sites More sharing options...
taith Posted June 7, 2007 Author Share Posted June 7, 2007 Ever thought of using the tidy extension? PS; Indexing a string via $string{3} is being depericated on favour of $string[3]. no... i need/want to be able to do this on the fly ;-) That is where the issue lies. you gotta figure out how to determine which needs to have \n after and which doesn't, which will probably need a manual array of known tags to not add the \n too. ya... thats the problem... i dont know how to determine if theres tags within the tags... in which i would include the next tag at the end... Quote Link to comment https://forums.phpfreaks.com/topic/54605-flatfile-autoformater/#findComment-270023 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 You will need http://en.wikipedia.org/wiki/recursion . =) EDIT::: www.php.net/recursive maybe ?? if not see the wikipedia entry. http://en.wikipedia.org/wiki/recursion Quote Link to comment https://forums.phpfreaks.com/topic/54605-flatfile-autoformater/#findComment-270029 Share on other sites More sharing options...
taith Posted June 7, 2007 Author Share Posted June 7, 2007 ? page isnt there... Quote Link to comment https://forums.phpfreaks.com/topic/54605-flatfile-autoformater/#findComment-270032 Share on other sites More sharing options...
taith Posted June 7, 2007 Author Share Posted June 7, 2007 ok... well... anybody know how to split between tags, without damaging anything? eg '<table><tr><td>text</td></tr></table>' becoming array('<table>','<tr>','<td>','text','</td>','</tr>','</table>') Quote Link to comment https://forums.phpfreaks.com/topic/54605-flatfile-autoformater/#findComment-270078 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 Half-assed: $tags = split(">", $tags); Just remember to add the > back onto the end of the line. Quote Link to comment https://forums.phpfreaks.com/topic/54605-flatfile-autoformater/#findComment-270079 Share on other sites More sharing options...
taith Posted June 7, 2007 Author Share Posted June 7, 2007 ya... i could just explode it... however... you get "text</td" Quote Link to comment https://forums.phpfreaks.com/topic/54605-flatfile-autoformater/#findComment-270082 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 Yea unfortunately without using regex etc that is how it has to be done, unless this is trictly for tables and there will always be a </td> you can always split that up. www.php.net/preg_match may work better as you can do something like find <td> (allchars here)</td> and it will return the results in an array (i believe) of the all chars here part. EDIT:: Found this on the user comments on www.php.net/preg_match <?php $string = "<td>text</td>"; $between = preg_match_between("<td>", "</td>", $string); echo $between; function preg_match_between($a_sStart, $a_sEnd, $a_sSubject) { $pattern = '/'. $a_sStart .'(.*?)'. $a_sEnd .'/'; preg_match($pattern, $a_sSubject, $result); return $result[1]; } Quote Link to comment https://forums.phpfreaks.com/topic/54605-flatfile-autoformater/#findComment-270083 Share on other sites More sharing options...
taith Posted June 7, 2007 Author Share Posted June 7, 2007 ya... i dont mind it splitting the text into its own segment in the array... i can join em back up... and preg_split() would prolly be the best/only viable way of doin it... i just dont get regex very well... LOL Quote Link to comment https://forums.phpfreaks.com/topic/54605-flatfile-autoformater/#findComment-270086 Share on other sites More sharing options...
taith Posted June 7, 2007 Author Share Posted June 7, 2007 i JUST built this... for those out there who wants it function explodebytags($string){ $out=array(); for($i=0; $i<=strlen($string); $i++){ if($string[$i]=='<'){ for($i=$i; $i<=strlen($string); $i++){ $temp.=$string[$i]; if($string[$i]=='>'){ $out[]=$temp; unset($temp); break; } } }else{ for($i=$i; $i<=strlen($string); $i++){ if($string[$i]=='<'){ $out[]=$temp; unset($temp); $i--; break; } $temp.=$string[$i]; } } } return $out; } Quote Link to comment https://forums.phpfreaks.com/topic/54605-flatfile-autoformater/#findComment-270100 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.