Skip to content

Instantly share code, notes, and snippets.

View lumonald's full-sized avatar
🏠
Working from home

Luke McDonald lumonald

🏠
Working from home
View GitHub Profile
@lumonald
lumonald / str_contains.php
Created October 12, 2017 14:32
PHP if string contains
<?php
$str = 'How are you?';
if (strpos($str, 'are') !== false) {
echo 'true';
}
?>
@lumonald
lumonald / get_ip.php
Created October 12, 2017 14:30
quickly get user IP
<?php
$IP = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP);
?>
@lumonald
lumonald / regex.txt
Created October 3, 2017 08:54
Komodo edit - Find/replace old PHP short opening tags Regex
Find:
<\?(?!php)
Replace:
<?php
@lumonald
lumonald / html_email_send_template.php
Created September 21, 2017 16:34
PHP HTML email with minimal styling
<?php
$to = '';
$subject = '';
$headers = "From: info@domain.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
//headers .= "Bcc: other@domain.com\r\n";
$flag = '';
$email_msg_top = "
@lumonald
lumonald / get_trimmed_str.php
Created September 21, 2017 16:21
Return trimmed string
<?php
function get_trimmed($str, $char_count){
$str_trimmed = strlen($str) > $char_count ? substr($str, 0, $char_count)."..." : $str;
return $str_trimmed;
}
?>
@lumonald
lumonald / array_to_csv.php
Created September 21, 2017 16:13
Write array to CSV
<?php
$array = array('val1', 'val2', 'val3');
$file = fopen("file.csv","w");
fputcsv($file, array("col1", "col2", "col3") );
foreach($array as $val){
fputcsv($file, array("$val") );
}
@lumonald
lumonald / db.php
Last active September 21, 2017 16:11
MySQLi quick connect
<?php
define('DB_HOST', 'localhost');
define('DB_NAME', '');
define('DB_USERNAME', '');
define('DB_PW', '');
$db_conn = mysqli_connect(DB_HOST, DB_USERNAME, DB_PW, DB_NAME);
if(mysqli_connect_errno()){
die('MySQL database connection failed. '.mysqli_connect_error(). ' --- '.mysqli_connect_errorno());
@lumonald
lumonald / curl_get_contents.php
Last active September 21, 2017 16:10
Alternative to file_get_contents() for some server environments
<?php
// file_get_contents() alternative for some server environments
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);