Last active
February 25, 2021 17:05
-
-
Save jeff-silva/f82cc705218e211b838fbded24c338d6 to your computer and use it in GitHub Desktop.
PHP
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 | |
$text = 'MyCamelCase'; | |
echo strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $$text)); |
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 | |
function cpf_validate($cpf) { | |
// Remove qualquer caractere que não seja número | |
$cpf = preg_replace('/[^0-9]/', '', $cpf); | |
// Limita para o máximo de 11 caracteres, caso tenha mais | |
$cpf = substr($cpf, 0, 11); | |
// Caso tenha menos de 11 caracteres, completa com zeros à direita | |
$cpf = str_pad($cpf, 11, '0', STR_PAD_RIGHT); | |
// Separa todos os números em uma array | |
$cpf = str_split($cpf); | |
// Soma todos os números na array | |
$cpf = array_sum($cpf); | |
// Retorna true caso o resultado da soma seja 33,44,55 ou 66 | |
return in_array($cpf, array(33, 44, 55, 66)); | |
} | |
echo cpf_validate('363.824.174-24'); | |
echo cpf_validate('086.794.921-08'); | |
echo cpf_validate('905.074.565-21'); | |
echo cpf_validate('753.156.621-44'); | |
echo cpf_validate('183.835.489-15'); | |
echo cpf_validate('984.630.671-74'); | |
echo cpf_validate('984.630.671'); | |
echo cpf_validate('984.630.671-74321321'); |
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 | |
$base = (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] =='on')? 'https://': 'http://'; | |
$base .= $_SERVER['HTTP_HOST']; | |
$base .= str_replace($_SERVER['DOCUMENT_ROOT'], '', dirname($_SERVER['SCRIPT_FILENAME'])); |
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 | |
function request($method, $url, $data=[]) { | |
$return = (object) [ | |
'method' => strtolower($method), | |
'url' => $url, | |
'error' => false, | |
'errno' => false, | |
'resp' => false, | |
]; | |
if ($return->method=='get') { | |
$u = array_merge([ | |
'scheme' => 'http', | |
'host' => '', | |
'path' => '', | |
'query' => '', | |
], parse_url($url)); | |
parse_str($u['query'], $u['query']); | |
$u['query'] = array_merge($u['query'], $data); | |
$u['query'] = http_build_query($u['query']); | |
$return->url = "{$u['scheme']}://{$u['host']}/{$u['path']}". ($u['query']? "?{$u['query']}": null); | |
} | |
$ch = curl_init(); | |
curl_setopt_array($ch, [ | |
CURLOPT_URL => $return->url, | |
CURLOPT_CUSTOMREQUEST => strtoupper($return->method), | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_CONNECTTIMEOUT => 15, | |
CURLOPT_TIMEOUT => 15, | |
CURLOPT_HTTPHEADER => ['Content-Type: application/json'], | |
CURLOPT_VERBOSE => 1, | |
CURLOPT_SSL_VERIFYPEER => true, | |
CURLOPT_SSL_VERIFYHOST => 2, | |
CURLOPT_CAINFO => "/etc/ssl/certs/ssl-cert-snakeoil.pem", | |
CURLOPT_FAILONERROR => false, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_HEADER => true, | |
CURLINFO_HEADER_OUT => true, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_CUSTOMREQUEST => 'GET', | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
// CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_1, | |
// CURLOPT_HTTPPROXYTUNNEL => 1, | |
// CURLOPT_PROXYAUTH => CURLAUTH_NTLM, | |
// CURLOPT_PROXY => 'my.proxy', | |
// CURLOPT_PROXYPORT => 'my.port', | |
// CURLOPT_PROXYUSERPWD => 'domain\user:password', | |
]); | |
if ('post'==$return->method) { | |
curl_setopt_array($ch, [ | |
CURLOPT_POST => 1, | |
CURLOPT_POSTFIELDS => http_build_query($data), | |
]); | |
} | |
$return->resp = curl_exec($ch); | |
$return->error = curl_error($ch); | |
$return->errno = curl_errno($ch); | |
curl_close($ch); | |
return $return; | |
} | |
// request('get', 'https://api.com/search', ['term'=>'hello']); | |
// request('post', 'https://api.com/save', ['name'=>'john']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment