Skip to content

Instantly share code, notes, and snippets.

@guimadaleno
guimadaleno / add-twitter-links.php
Last active March 8, 2022 18:25
Quickly add hyperlinks to mentions and hashtags on tweets using PHP
@guimadaleno
guimadaleno / stringAndRegex.php
Last active March 8, 2022 18:30
How to easily remove characters using RegEx and PHP
<?php
# Removing symbols
$example1 = "String with letters, numbers 0123456789 and symbols !@#$%¨&*()_+";
echo preg_replace("/[^a-zA-Z0-9\s]/", "", $example1); // String with letters numbers 0123456789 and symbols
# Remove symbols, including spaces
$example2 = "String with letters, numbers 0123456789 and symbols !@#$%¨&*()_+";
@guimadaleno
guimadaleno / brazilStates.html
Last active June 8, 2019 15:22
Select com os estados do Brasil - Select with Brazilian states
<select>
<option value="">Selecione</option>
<option value="AC">Acre</option>
<option value="AL">Alagoas</option>
<option value="AP">Amapá</option>
<option value="AM">Amazonas</option>
<option value="BA">Bahia</option>
<option value="CE">Ceará</option>
<option value="DF">Distrito Federal</option>
<option value="ES">Espirito Santo</option>
@guimadaleno
guimadaleno / regex-samples.php
Last active March 8, 2022 18:30
Simple ReGex and PHP examples to validate data
<?php
# Validate usernames between 4-28 chars
$string = "userNaME4234432_";
if (preg_match('/^[a-z\d_]{4,28}$/i', $string))
echo "example 1 successful.";
# Validate phone numbers format (XXX)XXX-XXXX
@guimadaleno
guimadaleno / post-without-curl.php
Last active March 8, 2022 18:33
PHP function to send GET/POST requests without cURL
<?php
# Function
function post_without_curl ($url, $data, $optional_headers = null)
{
$params = array('http' => array
(
'method' => 'POST',
'content' => http_build_query($data, "", "&")
@guimadaleno
guimadaleno / header-samples.php
Last active March 8, 2022 18:32
Some header manipulations using PHP
<?php
# 301 Moved Permanently (redirect)
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.domain.com/newpage.html');
# 404 Page Not Found
header('HTTP/1.1 404 Not Found');
@guimadaleno
guimadaleno / validar-cpf.php
Last active March 8, 2022 18:20
Função simples para validar CPF (autor desconhecido)
<?php
function is_cpf_valid ($cpf)
{
$cpf = preg_replace("/[^0-9]/", "", $cpf);
$digitoUm = 0;
$digitoDois = 0;
for($i = 0, $x = 10; $i <= 8; $i++, $x--):
$digitoUm += $cpf[$i] * $x;
endfor;
@guimadaleno
guimadaleno / find-and-replace-multiple-rows.sql
Last active March 8, 2022 18:31
Find and replace string data on multiple rows
UPDATE `table` SET `column` = REPLACE(`column`, 'origin', 'destiny');
@guimadaleno
guimadaleno / error-reporting-types.php
Last active March 8, 2022 18:31
Make PHP show or hide parse errors, notices and warnings
<?php
# Exibe erros de parsing, exceto notices e warnings
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
# Exibe erros de parsing e warnings, exceto notices
error_reporting(E_ALL ^ E_NOTICE);
@guimadaleno
guimadaleno / load-installed-timezones.php
Last active March 8, 2022 18:31
List all timezones in PHP
<?php
$tzList = DateTimeZone::listIdentifiers(DateTimeZone::ALL);
var_dump($tzList);