1. Validar Email
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
echo preg_match('/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix', $email) ? 'Válido' : 'Inválido'; |
2. Converter Endereço IP para um número inteiro e vice-versa(útil para armazenamento)
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
echo ip2long('192.168.0.58'); | |
echo long2ip('3232235578'); |
3. Filtrar uma lista de números(usa closures)
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
array_map(function($n){echo $n > 3 ? "$n\n" : '';}, array(1, 2, 3, 4, 5, 6)); |
4. Ler um arquivo
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
echo file_get_contents('arquivo.txt'); |
5. Somar uma lista de números
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
echo array_sum(array(5, 1, 7, 9.9, 0.1, 19)); |
6. Achar o menor ou maior número de uma lista
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
echo min(array(5, 8, 1, 3, -2)); | |
echo max(array(5, 8, 1, 3, -2)); |
7. Gerar e imprimir o alfabeto
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
array_map(function($l){echo "$l\n";}, range('a', 'z')); |
8. Imprimir as linhas de um arquivo ignorando as linhas vazias
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
foreach(file('arquivo.txt', FILE_IGNORE_NEW_LINES) as $l) empty($l) ? '' : print "$l\n"; |
9. Gerar uma senha de 8 caracteres aleatória
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
foreach(range(1,8) as $v) print chr(rand(97, 122)); |
10. Comprimir e descomprimir uma string muito longa
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
$string_comprimida = gzcompress($string_longa); | |
$string_longa = gzuncompress($string_comprimida); |
Espero que seja útil.