<?php
// using_tokeniser.php
// RJM Programming - December, 2024
// Thanks to https://www.php.net/manual/en/tokenizer.examples.php and https://www.php.net/manual/en/tidy.examples.php

$html_fragment='';
$wasf=$html_fragment;
if (isset($_GET['html'])) {
  $html_fragment=str_replace('+',' ',urldecode($_GET['html']));
  $tokens = token_get_all($html_fragment);
  foreach ($tokens as $token) {
    if (is_array($token)) {
        echo "Line {$token[2]}: ", token_name($token[0]), " ('{$token[1]}')", PHP_EOL;
    }
  }
  exit;
} else if (isset($_POST['html'])) {
  if (1 == 1) {
  $html_fragment=base64_decode($_POST['html']);
  } else {
  $html_fragment=str_replace('+',' ',urldecode($_POST['html']));
  }
} else {
  $html_fragment="<html>\n<head>\n<scr" . "ipt type=text/javascript>\n// Here is a Javascript comment\n var thevar=0;\n</scr" . "ipt>\n</head>\n<body>\n<p>Well, I never.  This is HTML.</p>\n<?php" . "\n// This is a comment " . "\n\n/* This is a second style of \n comment */\n \$phpvar='This is PHP'; \n for (\$i=0; \$i < strlen(\$phpvar); \$i++) {\n  echo substr(substr(\$phpvar, \$i),0,1); \n }\n?>\n</body>\n</html>\n";
  $wasf=$html_fragment;
}

$hprefix="<html><head><title>Using PHP Tokeniser Extension - RJM Programming - December, 2024</title><style> th { vertical-align: top; } td { vertical-align: top; } </style><scr" . "ipt type=text/javascript>
var orig='';
var lastbtoa='';

function ShowSelection() {  // thanks to https://stackoverflow.com/questions/7398847/detecting-if-textarea-content-is-selected-using-javascript
  var codelinei=0;
  var textComponent = document.getElementById('html');
  var selectionReset = textComponent.value;
  var selectedText;
  // Internet Explorer version
  if (document.selection != undefined) {
    textComponent.focus();
    var sel = document.selection.createRange();
    selectedText = sel.text;
  } else if (textComponent.selectionStart != undefined) { // Mozilla version
    var startPos = textComponent.selectionStart;
    var endPos = textComponent.selectionEnd;
    selectedText = textComponent.value.substring(startPos, endPos)
  }
  if (selectedText != lastbtoa && selectedText != '' && (selectedText.indexOf(String.fromCharCode(10)) != -1)) { // || selectedText.replace('<','').replace('>','').replace('(','').replace(')','').replace('{','').replace('}','').replace('[','').replace(']','') != selectedText)) {
   if ((selectedText.split('{').length == selectedText.split('}').length) && (selectedText.replace(/\ \<\ /g,'').replace(/\ \<\=\ /g,'').replace(/\ \>\ /g,'').replace(/\ \>\=\ /g,'').split('<').length == selectedText.replace(/\ \<\ /g,'').replace(/\ \<\=\ /g,'').replace(/\ \>\ /g,'').replace(/\ \>\=\ /g,'').split('>').length) && (selectedText.split('[').length == selectedText.split(']').length) && (selectedText.split('(').length == selectedText.split(')').length)) {
    codelinei=eval('' + selectedText.split(String.fromCharCode(10)).length);
    document.getElementById('singleline').rows='' + codelinei;
    document.getElementById('singleparse').rows='' + codelinei;
    //alert(codelinei);
    document.getElementById('singleline').style.display='block';
    document.getElementById('singleline').value=selectedText;
    document.getElementById('singleparse').style.display='block';
    lastbtoa=selectedText;
    document.getElementById('singleparse').src='./using_tokeniser.php?html=' + encodeURIComponent(selectedText);
    textComponent.value=selectionReset;
    document.getElementById('tidyhtml').focus();
    setTimeout(ShowSelection, 5000);
   } else {
    setTimeout(ShowSelection, 1000);
   }
  } else {
    setTimeout(ShowSelection, 1000);
  }
}

function btoait() {
  orig=document.getElementById('html').value;
  document.getElementById('html').value=window.btoa(orig);
  return true;
}

setTimeout(ShowSelection, 1000);
</scr" . "ipt>
</head><body>";
$hform="<h1>Using Tokeniser</h1><h3>RJM Programming - December, 2024</h3><h4>Thanks to <a target=_blank title='https://www.php.net/manual/en/tokenizer.examples.php' href='//www.php.net/manual/en/tokenizer.examples.php'>https://www.php.net/manual/en/tokenizer.examples.php</a></h4><br<br><form onsubmit='return btoait();' action=./using_tokeniser.php method=POST>

<table border=2 style=width:98%;>
<tr><th style=width:50%;>Your PHP<br><textarea id=singleline style=display:none;background-color:yellow;width:98%;></textarea></th><th style=width:50%; id=thmode>Output PHP<br><iframe id=singleparse style=display:none;width:98%;background-color:yellow; src=/About_Us.html></iframe></th></tr>
<tr><td><textarea title='Highlight line(s) you want to tokenise.' style=width:98%;height:450px; name=html id=html>" . $html_fragment . "</textarea></td><td><textarea style=width:98%;height:450px;background-color:#f0f0f0; id=tidyhtml></textarea></td></tr>
</table>
<br>
<input type=submit value='Strip Out PHP Comments'></input>
</form>
";
$hsuffix="</body></html>";

if ($html_fragment != $wasf) {
if (1 == 1) {
$tokens = token_get_all($html_fragment);
$bs='';
foreach ($tokens as $token) {
   if (is_string($token)) {
       // simple 1-character token
       $bs.=$token;
   } else {
       // token array
       list($id, $text) = $token;

       switch ($id) { 
           case T_COMMENT: 
           case T_DOC_COMMENT:
               // no action on comments
               break;

           default:
               // anything else -> output "as is"
               $bs.=$text;
               break;
       }
   }
} 
$hform=str_replace("></textarea></td></tr>", ">" . $bs . "</textarea></td></tr>", $hform);
} else {
$tidy_config = array(
                     'clean' => true,
                     'output-xhtml' => true,
                     'show-body-only' => true,
                     'wrap' => 0,
                     );

$tidy = tidy_parse_string($html_fragment, $tidy_config, 'UTF8');
$tidy->cleanRepair();
$hform=str_replace("></textarea></td></tr>", ">" . $tidy . "</textarea></td></tr>", $hform);
}
}

echo $hprefix . $hform . $hsuffix;

?>
