Created
April 9, 2020 08:31
-
-
Save vfontjr/6874a6f1d256900575b45fc8c71551d2 to your computer and use it in GitHub Desktop.
Working Example of Code to process a text file and convert to csv to import into Formidable Forms.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$file_to_process = "raw-content.csv"; | |
$processed_functions = "processed_content.csv"; | |
$header_record = '"Name","Entry Type","Formidable Pro","Function Parent Class","File Location","Function Declaration"'; | |
$fh_src = fopen($file_to_process, 'r'); | |
$fh_tgt = fopen($processed_functions, 'w'); | |
$r = fwrite($fh_tgt, $header_record . PHP_EOL); | |
if ($fh_src === false) { | |
exit("Cannot open file for reading\n"); | |
} | |
echo 'starting loop' . '<br/>'; | |
while (!feof($fh_src)) { | |
$line = fgets($fh_src); | |
if ($line === false) { | |
exit("Cannot read from file\n"); | |
} | |
$func_name = get_name( $line ); | |
$entry_type = "Function"; | |
$is_pro = "No"; | |
$parent_class = get_parent( $line ); | |
$file_loc = get_file_loc( $line ); | |
$declaration = get_declaration( $line ); | |
/* | |
echo 'name: ' . $func_name . '<br/>'; | |
echo 'type: ' . $entry_type . '<br/>'; | |
echo 'is_pro: ' . $is_pro . '<br/>'; | |
echo 'parent: ' . $parent_class . '<br/>'; | |
echo 'loc: ' . $file_loc . '<br/>'; | |
echo 'dec: ' . $declaration . '<br/>'; | |
*/ | |
$new_line = process_line( $func_name, $entry_type, $is_pro, $parent_class, $file_loc, $declaration ); | |
$r = fwrite($fh_tgt, $new_line . PHP_EOL); | |
} | |
fclose($fh_src); | |
fclose($fh_tgt); | |
echo 'all done' . '<br/>'; | |
function get_name( $line ) { | |
$start_pos = strrpos( $line, 'function' ) + 8 ; | |
$paren_count = substr_count($line, '(', $start_pos ); | |
// echo 'paren count: ' . $paren_count . '<br/>'; | |
if ( $paren_count > 1 ) { | |
$end_pos = strpos( $line, '(', $start_pos ); | |
} else { | |
$end_pos = strrpos( $line, '(' ); | |
} | |
$offset = (strlen($line)-$end_pos); | |
return substr( $line, $start_pos+1, $end_pos-$start_pos-1); | |
} | |
function get_parent( $line ) { | |
$start_pos = strrpos( $line, '/', -10 ); | |
$end_pos = strpos( $line, '.' ); | |
return substr( $line, $start_pos+1, $end_pos-$start_pos-1); | |
} | |
function get_file_loc( $line ) { | |
$end_pos = strpos( $line, '(' ); | |
return substr( $line, 0, $end_pos); | |
} | |
function get_declaration( $line ) { | |
$start_pos = strpos( $line, ' ' ); | |
return trim(substr( $line, $start_pos )); | |
} | |
function process_line( $func_name, $entry_type, $is_pro, $parent_class, $file_loc, $declaration ) { | |
$terminator = '"'; | |
$separator = '","'; | |
$new_line = $terminator . $func_name . $separator . $entry_type . $separator . $is_pro . $separator . $parent_class . $separator . $file_loc . $separator . $declaration . $terminator; | |
return $new_line; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment