Jump to content

Recommended Posts

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);

Link to comment
https://forums.phpfreaks.com/topic/54605-flatfile-autoformater/
Share on other sites

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...

Link to comment
https://forums.phpfreaks.com/topic/54605-flatfile-autoformater/#findComment-270023
Share on other sites

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];
}

Link to comment
https://forums.phpfreaks.com/topic/54605-flatfile-autoformater/#findComment-270083
Share on other sites

i JUST built this... for those out there who wants it :D

 

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;
}

Link to comment
https://forums.phpfreaks.com/topic/54605-flatfile-autoformater/#findComment-270100
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.