Advertisement
plirof2

csv2html.php v014d remove_unwanted_lines autolink order_lesson PARSER sch links - make href

May 23rd, 2021 (edited)
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.67 KB | None | 0 0
  1. <?php
  2. //v014d 230124 fixed: Show previous and nect entries
  3. //v014c 230122 - Replacing $SERVER variable also
  4. //v014b - added remove_unwanted_lines() ,$ignore_bash_script_unwanted_lines=true
  5. //v014a - added <li> (maybe not usefull)
  6. //v013b get adjest week
  7. //v011 autolink !!
  8. //v010 seems to work
  9. //v009
  10. //v008
  11. //v004
  12. //v001
  13.  
  14. // Desc: Convert john_start_kill bash to html links (shows only near weeks)
  15.  
  16.  
  17. /*
  18.  
  19. ALSO try these :
  20. http://code.seebz.net/p/autolink-php/  ****
  21. javascript http://code.seebz.net/p/autolink-js/
  22.  
  23. */
  24.  
  25. $file_to_parse="./john_start_kill_apps.sh";
  26. #$file_to_parse="./order_lesson.txt";
  27.  
  28. $delimiters = array('|_|'); // How each line is divided
  29. $count=0;
  30.  
  31.  
  32. $ignore_bash_script_unwanted_lines=true; // ignores lines starting with # (for .sh scripts)
  33. $weekofyear = date("W");
  34. echo $weekofyear;
  35. $check_week=true; // show week of year only values
  36.  
  37. //REPLACEMENTS
  38. //$SERVER_current="http://192.168.1.200/"; // doesn't work check function replaceDelimiters()
  39.  
  40.  
  41. function remove_unwanted_lines($str){
  42.     //Pass1: Remove lines starting with #
  43.     //pass2: Keep lines with adjust and http
  44.     // if ($ignore_bash_script_unwanted_liness)
  45.     //if($data[$c][0]"#")echo "HELLO";//NOT WORKING
  46.     //$str=$data[$c]
  47.     //echo("<h3>$str</h3><hr size=10>");
  48.  
  49.  
  50.     if(strpos($str,"adjustWeekFinalNum")!==false){
  51.             //return "HELLO"    ;
  52.             return '<h3>'.$str.'</h3>';
  53.  
  54.     }
  55.     if(strpos($str,"#")===0 )return "IGNORED";
  56.  
  57.         if(strpos($str,"http")!==false){
  58.             //return "HELLO"    ;
  59.             return $str;
  60.  
  61.         }
  62.  
  63.     if(strpos($str,"#")!=0 || strpos($str,"#")===false ){
  64.         if(strpos($str,"http")!==false){
  65.             //return "HELLO"    ;
  66.             return $str;
  67.  
  68.         }
  69.            
  70.     }// END of if(strpos($str,"#")==0){
  71.     return "IGNORED";
  72.     return $str;      
  73.  
  74. }
  75.  
  76.  
  77. function autolink($str, $attributes=array()) {
  78.     $attrs = '';
  79.     foreach ($attributes as $attribute => $value) {
  80.         $attrs .= " {$attribute}=\"{$value}\"";
  81.     }
  82.  
  83.     $str = ' ' . $str;
  84.  
  85.     $replacement_string='<li>'.'$1<a target=_blank href="$2"'.$attrs.'>$2</a></li>';
  86.     //$replacement_string=$replacement_string."\n";
  87.  
  88.  // ORIG ok working jon 210525
  89.     $str = preg_replace(
  90.         '`([^"=\'>])((http|https|ftp)://[^\s<]+[^\s<\.)])`i',
  91.          $replacement_string,
  92.         $str
  93.     );
  94.  
  95.  
  96.     $str=$str."\n";
  97.     $str = substr($str, 1);
  98.    
  99.     return $str;
  100. }
  101.  
  102.  
  103.  
  104. //$string_of_file = file_get_contents('./order_lesson.txt', true);
  105.  
  106. $string_of_file = file_get_contents($file_to_parse, true);
  107.  
  108.  
  109. $string_modified=replaceDelimiters($string_of_file); //replaces delimites and bash variables
  110. //echo "$string_modified";
  111. //$string_modified=makeHref($string_modified); //problematic
  112.  
  113. //if ($ignore_bash_script_unwanted_lines) $string_modified=remove_unwanted_lines($string_modified);
  114.  
  115. //$string_modified=autolink($string_modified); //Seems to work !!!!!!!!
  116.  
  117.  
  118. if ($check_week){
  119.     $substring = "adjustWeekFinalNum_for_week";
  120.     ///$result = get_string_between($string_modified , $substring." ".($weekofyear-1), $substring." ".($weekofyear+2) //Orig
  121.     $result = get_string_between($string_modified , $substring." ".($weekofyear-1)."\n", $substring." ".($weekofyear+2)."\n" ); // 230124 Show previous and nect entries
  122.  
  123.     //echo "<h1> <hr>".$substring." ".($weekofyear-1)."<hr>".$substring." ".($weekofyear+2)."<hr>".$result."</h1>" ;
  124.     $string_modified=$result;
  125. }
  126.  
  127.  
  128.  
  129. //$string_modified=formatUrlsInText($string_modified); //Seems to work ok issues with some splitting
  130.  
  131.  
  132. //str_replace($search, $replace, $subject);
  133.  
  134. //$string_modified=str_replace('[', '<BR>', $string_modified); echo("ZZZZZZZZZ".$string_modified); //DEBUG
  135. //echo "<hr size =100>";
  136.  
  137.  
  138.  
  139. $AllData = str_getcsv($string_modified, "\n"); //parse the rows
  140. //print_r($AllData);
  141.  
  142.  
  143.  echo '<ol type="1"> <table border="1"> ';
  144.  
  145. //exit ();
  146. //=============== FORMAT to tables ===========================
  147. $row = 2;
  148.  
  149.  
  150.  
  151.  
  152.  
  153. //foreach($AllData as &$Row) $Row = str_getcsv($Row, "[") {
  154. foreach($AllData as $data1) {
  155.         //echo "<h1>$data1</h1>";
  156.  
  157.  
  158.         if ($ignore_bash_script_unwanted_lines) $data1=remove_unwanted_lines($data1);
  159.  
  160.         if($data1=="IGNORED") continue;
  161.         $data1=autolink($data1); //Seems to work !!!!!!!!
  162.         $data = str_getcsv($data1, "[")   ;  
  163.        
  164.         //print_r($data);
  165.        
  166.  
  167.         $num = count($data);
  168.         //echo "<h1>num=$num ________  data1=$data1</h1>";
  169.  
  170.  
  171.         if ($row == 1) {
  172.             echo '<thead><tr>';
  173.         }else{
  174.             echo '<tr>';
  175.         }
  176.        
  177.         for ($c=0; $c < $num; $c++) {
  178.            // if ($ignore_bash_script_unwanted_liness) if($data[$c][0]"#")echo "HELLO";//NOT WORKING
  179.             //if ($ignore_bash_script_unwanted_lines) $data[$c]=remove_unwanted_lines($data[$c]);
  180.             //if($data[$c]=="IGNORED") continue;
  181.             //echo $data[$c] . "<br />\n";
  182.             if(empty($data[$c])) {
  183.                $value = "&nbsp;";
  184.             }else{
  185.                $value = $data[$c];
  186.             }
  187.             if ($row == 1) {
  188.                 echo '<th>'.$value.'</th>';
  189.             }else{
  190.                 echo '<td>'.$value.'</td>'."\n";
  191.             }
  192.         }
  193.        
  194.         if ($row == 1) {
  195.             echo '</tr></thead><tbody>';
  196.         }else{
  197.             echo '</tr>';
  198.         }
  199.         $row++;
  200.  
  201. }
  202.  //parse the items in rows
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210. /**
  211.  * Will replace a number of CSV delimiters to one specific character
  212.  * AND replaces bash variables
  213.  * @param $file     CSV file
  214.  */
  215. //function replaceDelimiters($str,$prefixserverurl='http://192.168.1.200/')
  216. function replaceDelimiters($str,$prefixserverurl='http://192.168.1.200/')
  217. {
  218.     // Delimiters to be replaced: pipe, comma, semicolon, caret, tabs
  219.    //$delimiters = array('|', ';', '^', "\t");
  220.  
  221.     //$delimiters = array('|_|');
  222.     global $delimiters;
  223.     $delimiter = '[';
  224.     //str_replace($search, $replace, $subject);
  225.     //$str = file_get_contents($file);
  226.     $str = str_replace($delimiters, $delimiter, $str);
  227.  
  228.  
  229.     $str = str_replace('""$SWFlocal"', $prefixserverurl."swf/", $str);
  230.     //$str = str_replace(' "$SWFlocal"', " http://192.168.1.200/swf/", $str);
  231.     $str = str_replace(' "$SWFlocal"', " http://192.168.1.200/swf/", $str);
  232.     $str = str_replace('"$SWFpath"', $prefixserverurl."swf/", $str);
  233.     $str = str_replace('$SWFgiortes"', $prefixserverurl."swf/swf_giortes/", $str);
  234.     $str = str_replace('$RAMKIDpathprefix"', $prefixserverurl."ramkid/", $str);
  235.     $str = str_replace('lightbot_iron_browser "', $prefixserverurl."gamesedu/lightbot_haan/index.html?map=", $str);
  236.     $str = str_replace('""$GAMESEDU"', $prefixserverurl."gamesedu/", $str);
  237.     $str = str_replace('"$GAMESEDU"', $prefixserverurl."gamesedu/", $str);
  238.     $str = str_replace('"$SERVER"', $prefixserverurl, $str);
  239.  
  240.     $str = str_replace('ironstart ', "", $str);
  241.     $str = str_replace('ironstartincognito ', "", $str);
  242.  
  243.  
  244.     $str = str_replace('\&', "&", $str);
  245.     $str = str_replace('"', " ", $str);
  246.    
  247.  
  248.     $str = str_replace('html"', "html", $str); // IMPORTANT !!!
  249.  
  250.  
  251.  
  252.    
  253.  
  254.  
  255.  
  256.     $str = str_replace('aaaaaaa', "", $str);
  257.     $str = str_replace('aaaaaaa', "", $str);
  258.     $str = str_replace('aaaaaaa', "", $str);
  259.  
  260.  
  261.     $delimiters = array('|_|');
  262.     $delimiter = '[';
  263.  
  264.  
  265.  
  266.     //file_put_contents($file, $str);
  267.     return $str;
  268. }
  269.  
  270.  
  271.  
  272.  
  273. function get_string_between($string, $start, $end){
  274.     global $weekofyear,$check_week;
  275.  
  276.  
  277.  
  278.     $string = ' ' . $string;
  279.     $ini = strpos($string, $start);
  280.     if ($ini == 0) return '';
  281.     $ini += strlen($start);
  282.     $len = strpos($string, $end, $ini) - $ini;
  283.     return substr($string, $ini, $len);
  284. }
  285.  
  286. ?>
  287.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement