<?php
// code / comments
function delete_comments( $c, $m ) {
/* USE:
$m = 's'; // 's' or 'm'
$c = delete_comments( $c, $m );
*/
/* META TAGS:
ALGO: medium
BETA: no
DATE: 2025-02-18 00:45
DESC: deletes comments (single line, multi-line)
PROC: no
TAGS: code, string
*/
$l = strlen( $c ); // string length
$a = str_split( $c ); // get chars array
$sw = 1; // switch record mode on
$r = ""; // result
for ( $i=0; $i<$l; $i++ ) { // loop through chars
// evaluate record mode switch on/off
if ( $m == "s" ) { // mode "single line comment"
if (
( $a[$i] == "/" ) && // this
( $a[$i+1] == "/" ) // next
) $sw = 0; // switch record mode off
else if (
$a[$i] == "\n" // this
) $sw = 1; // ... on
} else if ( $m == "m" ) { // mode "multi-line comment"
if (
( $a[$i] == "/" ) && // this
( $a[$i+1] == "*" ) // next
) $sw = 0; // switch record mode off
else if (
( $a[$i-2] == "*" ) && // prev. prev.
( $a[$i-1] == "/" ) // prev.
) $sw = 1; // ... on
} // if
// add char
if ( $sw == 1 ) $r .= $a[$i]; // add char to result
} // for
return $r;
} // function
// code / blankspaces and tabulators
delete_useless_blankspaces_and_tabulators( $c ) {
/* USE:
$c = delete_useless_blankspaces_and_tabulators( $c );
*/
/* META TAGS:
ALGO: medium
BETA: no
DATE: 2025-03-04 17:45
DESC: see function name
PROC: no
TAGS: code, string
*/
$l = strlen( $c ); // get string length
$a = str_split( $c ); // get array from string
$sw = 0; // switch record mode off
$cr = ""; // result code reversed
for ( $i=$l-1; $i>=0; $i-- ) { // loop through string (string array) in reversed way
$ch = $a[$i]; // current char
if !(
( $ch == " ") ||
( $ch == "\t" )
) {
$sw = 1; // switch record mode on
} else if ( $ch == "\n" ) {
$sw = 0; // switch record mode off
$cr .= "\n"; // ... but record "\n" char
} // if/else
if ( $sw == 1 ) $cr.= $ch; // record char
} // for
$r = strrev( $cr ); // reverse string
return $r;
} // function
// code / empty lines
function delete_empty_lines( $c, $m ) {
/* USE:
$c = delete_empty_lines( $c, $m );
*/
/* META TAGS:
ALGO: medium
BETA: yes
DATE: 2025-03-10 18:10
DESC: see function name
PROC: no
TAGS: code, string
*/
$a = explode( "\n", $c ); // lines array
$lc = count( $a ); // lines counter
$ec = 0; // empty lines counter
for ( $i=0; $i<$lc; $i++ ) { // loop through lines
$li = $a[$i]; // curr. line
$ll = strlen( $li ); // get line length
$b = str_split( $li ); // get array from string
$sw = 0; // "normal" char line (not containing " " or "\t") switch off
$r = "";
for ( $j=0; $j<$lc; $j++ ) { // loop through chars of line
$ch = $b[$j]; // curr. char
if !(
( $ch == " " ) ||
( $ch == "\t" )
) {
$sw = 1; // "normal" char line switch on
$ec = 0; // empty lines counter "0"
} // if
} // for
if ( $sw == 0 ) $ec++; // count empty lines counter up
if ( $ec <= $m ) $r .= $li; // add line to result string
} // for
return $r;
} // function
function delete_useless_empty_lines( $c ) {
$m = 1; // max.
$r = delete_empty_lines( $c, $m );
return $r;
}
function delete_all_empty_lines( $c ) {
$m = 0; // max.
$r = delete_empty_lines( $c, $m );
return $r;
}
?>