Skip to content

Instantly share code, notes, and snippets.

@thepsion5
thepsion5 / html_attr_extraction_functions.php
Created February 4, 2016 15:32
Simple PHP functions to extract links and metadata from a webpage
<?php
//Created as an alternative to an article demonstrating the use of regular expressions to parse HTML:
// http://www.web-development-blog.com/archives/parse-html-with-preg_match_all/
/**
* @param string $remoteUrl
* @param string $yourLink
* @return bool
*/
function check_back_link($remoteUrl, $yourLink)
@thepsion5
thepsion5 / LaravelValidator.php
Last active November 10, 2016 07:25
Demonstration of a simple wrapper for Laravel's Validator that allows rules to be altered based on input before validating and exceptions as transports for validation errors
<?php
use Illuminate\Support\MessageBag,
Illuminate\Validation\Factory;
/**
* A simple wrapper for Laravel's native validator. Allows
* validation rules to be modified pre-validation based on
* input and can be used to throw an exception with the
* input and validation failures
@thepsion5
thepsion5 / Model.js
Created January 13, 2017 16:07
Legacy Javascript from a SPA written in 2007
//Vintage Jquery 1.3
$.post("DataFiles/Registration.ashx",
{
//Actual values sent to the server are formatted are broken into 3 segments, separated by a tilde
//The first is the type to which the value should be cast at the server, the second the actual value
//The third segment, "fin" specifies the server should not validate or update those fields
//We have the best code. Tremendous code, people tell me all the time.
new_ncoqfrname: $("#txtRegNo").attr("type") + "~" + $("#txtRegNo").val() + "~fin",
new_federalidfein: $("#new_federalidfein").attr("type") + "~" + $("#new_federalidfein").val() + "~fin",
new_ncoqfrfinancialperiodstartdate: "datetime" + "~" + $("#new_ncoqfrfinancialperiodstartdate").val() + "~fin",
@thepsion5
thepsion5 / example.php
Created January 19, 2017 18:18
An extremely simple example of how to create a generic validation function using PHP's filter_input() function and/or callbacks
<?php
//Define how each POST field is validated
$fieldValidation = [
'username' => 'required',
'first_name' => 'required',
'last_name' => 'required',
'website' => 'website',
'email' => 'email',
'pass' => 'password',
@thepsion5
thepsion5 / whyyyyyyyy.php
Created May 16, 2017 19:11
Did you know you can shove entire chunks of HTML into a PHP array key? WELL YOU CAN
<?php
//Formatting and indentation unchanged from original file
$find = $_GET['find'];
$browse = $_GET['browse'];
//More awful code ommitted to preserve the sanity of readers
if(isset($find)) {
$find = addcslashes(mysql_real_escape_string($find), "%_");
$rs = new MySQLPagedResultSet("SELECT * FROM TableName WHERE MATCH (Name) AGAINST ('$find' IN BOOLEAN MODE) ORDER BY Name ASC", 100, $dbConnect);
}
@thepsion5
thepsion5 / GeneratesPermutationsTrait.php
Last active August 31, 2017 16:39
Example Permutation Generation
<?php
trait GeneratesPermutationsTrait
{
/**
* Generates an array with N unique combinations of inputs and a unique output, where N is equal to 2^count($params)
* For example, generatePermutations([], 'Param 1', 'Param 2') would return something similar to:
* [
* [
* 'inputs' => ['Param 1v1', 'Param 2v1'],
@thepsion5
thepsion5 / legacy-app-index.php
Created October 13, 2017 15:24
An example of how an external package containing pure domain logic can be cleanly integrated into different applications
<?php
//FILE: legacy-application/public/index.php
require_once __DIR__ . '/../vendor/autoload.php';
$config = require __DIR__ . '../config/settings.php';
$counties = require __DIR__ . '/../vendor/my-company/my-package/src/counties.php';
$app = \App\Application::createFromConfig($config, $counties);
@thepsion5
thepsion5 / Application.php
Created June 28, 2018 15:03
Example of a Small PHP Application Built Without A Framework
<?php
//FILENAME: /spaghetti-monster/src/Application.php
namespace SpaghettiMonster;
use SpaghettiMonster\Domain\EntityRepository;
use SpaghettiMonster\Domain\EntitySearch;
use SpaghettiMonster\Domain\EntityUpdater;
use SpaghettiMonster\Domain\Twitter\ApiClient;
use SpaghettiMonster\Domain\Twitter\TweetCreator;
@thepsion5
thepsion5 / ModuleAServiceProvider.php
Last active July 18, 2018 11:40
Simple Module Concept for Laravel 5
<?php
namespace App\Providers;
use Illuminate\Support\ModuleServiceProvider;
class FooModuleServiceProvider extends ModuleServiceProvider
{
protected $moduleName = 'Foo Module';
protected $moduleNamespaces = ['App\FooModule'];
@thepsion5
thepsion5 / pagination.php
Created September 3, 2018 18:02
A simple example of procedural PHP code that can be used to generated paginated results from a database query
<?php
require 'setup/connection.php'; //returns a PDO instance;
$searchTerm = isset($_GET['search']) ? $_GET['search'] : null;
$currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
//Step 1: Get the total number of results for a search
$sqlParams = [];
$totalResultSql = 'SELECT COUNT(*) AS count FROM articles ';
if ($searchTerm) {
$sqlParams['search'] = "%$searchTerm%";