Created
January 3, 2025 11:56
-
-
Save terremoth/7d6a1615f86c293aa01d6aa6b27573b3 to your computer and use it in GitHub Desktop.
Some useful PHP helper functions
This file contains 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 | |
// Don't just COPY everything to your codebase without looking the need of these functions. | |
// Helpers functions can be a cancer if you don't have any idea where to put and why. | |
const DS = DIRECTORY_SEPARATOR; | |
function echoln($str) { | |
echo $str . PHP_EOL; | |
} | |
function echobr($str) { | |
echo $str . '<br />'; | |
} | |
function isValidMd5($md5 = '') { | |
return preg_match('/^[a-f0-9]{32}$/', $md5); | |
} | |
function toDateBr($date) { | |
return implode("-", array_reverse(explode("/", $date))); | |
} | |
function delete_all_between($beginning, $end, $string) { | |
$beginningPos = strpos($string, $beginning); | |
$endPos = strpos($string, $end); | |
if ($beginningPos === false || $endPos === false) { | |
return $string; | |
} | |
$textToDelete = substr($string, $beginningPos, ($endPos + strlen($end)) - $beginningPos); | |
return str_replace($textToDelete, '', $string); | |
} | |
function dateTimeBrToEn($date) { | |
$d = explode(" ", $date); | |
$concat = ''; | |
if (isset($d[1])) { | |
$concat = ' '.$d[1]; | |
} | |
return implode("-", array_reverse(explode("/", $d[0]))) . $concat; | |
} | |
function removeAccents($str) { | |
$unwanted_array = array( | |
'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', | |
'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', | |
'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', | |
'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', | |
'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y' | |
); | |
$str = strtr($str, $unwanted_array); | |
return $str; | |
} | |
function removeSpecialChars($item) { | |
return preg_replace('/[\W_]/', '', $item); | |
} | |
function removeSpecialCharsUrl($str) { | |
$unwanted_array = array( | |
'!', '@', '$', '%', '', '*', '(', ')', '?', '/', ';', ':', | |
'#', '¨', '\\', '|', '<', '>', '[', ']', '{', '}', ',', '.', | |
'%', '&', '+', 'ª', 'º', '¹', '²', '³', '£', '¬', '§', '¢', '=' | |
); | |
$str = str_replace($unwanted_array, '', $str); | |
return $str; | |
} | |
function makeUrl($str) { | |
$str = removeAccents($str); | |
$str = removeSpecialCharsUrl($str); | |
$str = strtolower($str); | |
$str = preg_replace('/\s+\-\s+/', '-', $str); | |
$str = preg_replace( '/[`^~\'"]/', null, $str); | |
$str = preg_replace('/\s+/', '-', $str); | |
return iconv( 'UTF-8', 'ASCII//TRANSLIT', $str); | |
} | |
/** | |
* Credit card validation check in PHP | |
* | |
* Source: http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers | |
*/ | |
function luhnCheck($num) { | |
$str = ''; | |
foreach(array_reverse(str_split($num)) as $i => $c) { | |
$str .= (($i % 2) ? ($c * 2) : $c); | |
} | |
return array_sum(str_split($str)) % 10 == 0; | |
} | |
function formatarCpfCnpj($cnpj_cpf) { | |
if (strlen(preg_replace("/\D/", '', $cnpj_cpf)) === 11) { | |
$response = preg_replace("/(\d{3})(\d{3})(\d{3})(\d{2})/", "\$1.\$2.\$3-\$4", $cnpj_cpf); | |
} else { | |
$response = preg_replace("/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/", "\$1.\$2.\$3/\$4-\$5", $cnpj_cpf); | |
} | |
return $response; | |
} | |
function array_deep_sum($array, $field='') { | |
$sum = 0; | |
foreach ($array as $value) { | |
$sum += $value[$field]; | |
} | |
return $sum; | |
} | |
function validaCPF($cpf = null) { | |
// Verifica se um número foi informado | |
if(empty($cpf)) { | |
return false; | |
} | |
// Elimina possivel mascara | |
$cpf = preg_replace("/[^0-9]/", "", $cpf); | |
$cpf = str_pad($cpf, 11, '0', STR_PAD_LEFT); | |
// Verifica se o numero de digitos informados é igual a 11 | |
if (strlen($cpf) != 11) { | |
return false; | |
} | |
// Verifica se nenhuma das sequências invalidas abaixo | |
// foi digitada. Caso afirmativo, retorna falso | |
else if ($cpf == '00000000000' || | |
$cpf == '11111111111' || | |
$cpf == '22222222222' || | |
$cpf == '33333333333' || | |
$cpf == '44444444444' || | |
$cpf == '55555555555' || | |
$cpf == '66666666666' || | |
$cpf == '77777777777' || | |
$cpf == '88888888888' || | |
$cpf == '99999999999') { | |
return false; | |
// Calcula os digitos verificadores para verificar se o | |
// CPF é válido | |
} else { | |
for ($t = 9; $t < 11; $t++) { | |
for ($d = 0, $c = 0; $c < $t; $c++) { | |
$d += $cpf[$c] * (($t + 1) - $c); | |
} | |
$d = ((10 * $d) % 11) % 10; | |
if ($cpf[$c] != $d) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} | |
function execInBackground($cmd) { | |
if (PHP_OS_FAMILY === "Windows"){ | |
pclose(popen("start /B ". $cmd, "r")); | |
} | |
else { | |
exec($cmd . " > /dev/null &"); | |
} | |
} | |
function describeTable($tableName) { | |
return \Illuminate\Support\Facades\DB::select('DESCRIBE '.$tableName); | |
} | |
function tableColumns($tableName) { | |
return array_column(describeTable($tableName), 'Field'); | |
} | |
function tableTypes($tableName) { | |
return array_column(describeTable($tableName), 'Type'); | |
} | |
function tableColumnsWithTypes($tableName) { | |
return array_combine(tableColumns($tableName), tableTypes($tableName)); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment