Skip to content

Instantly share code, notes, and snippets.

@aa21
Last active October 18, 2016 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aa21/ac4004e02dd9022c1129 to your computer and use it in GitHub Desktop.
Save aa21/ac4004e02dd9022c1129 to your computer and use it in GitHub Desktop.
PHP - Aligning tabs, finding emails in string, remove empty columns from csv/data-grid array ..
<?php
// PRINT TAB ALIGNED COLUMNS
// %- and % are left and right aligns
$mask = "| %-30.30s | %10.10s | %10.10s |\n";
printf($mask, 'Host' , 'Results', 'Share');
printf($mask, $test_host, $total_results, $share_round);
// FIND ALL EMAILS IN A STRING
function find_all_emails($content){
$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
'\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
$quoted_pair = '\\x5c[\\x00-\\x7f]';
$domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
$quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
$domain_ref = $atom;
$sub_domain = "($domain_ref|$domain_literal)";
$word = "($atom|$quoted_string)";
$domain = "$sub_domain(\\x2e$sub_domain)*";
$local_part = "$word(\\x2e$word)*";
$addr_spec = "$local_part\\x40$domain";
preg_match_all("/$addr_spec/", $content, $matches);
return $matches[0];
}
credit: http://www.iamcal.com/publish/articles/php/parsing_email/
// remove empty 'columns' from a grid (array of arrays)
$emptyCols = [];
// remove empty columns
foreach ($data as $row => $rowData){
$firstRow = $row === 0;
foreach($rowData as $cell => $cellData){
if(empty($cellData) && $firstRow){
$emptyCols[] = $cell;
}elseif(!$firstRow && !empty($cellData) && in_array( $cell, $emptyCols)) {
unset($emptyCols[array_search($cell, $emptyCols)]);
}
}
}
rsort($emptyCols, SORT_NUMERIC);
foreach($emptyCols as $cell){
foreach($data as $row => $rowData){
unset($data[$row][$cell]);
$data[$row] = array_values($data[$row]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment