Wednesday, August 22, 2018

PHP Foreach Key by Pointer


Here is a way to add a new elements to a new array in PHP foreach function, by reference on a new element.


$items = Item::get();
$formattedItems = [];

foreach ($items as $item) {
    $fm = &$formattedItems[];
    $fm['id'] = $item->id;
    $fm['label'] = $item->label;
    $fm['icon'] = $item->icon;
}


var_dump($formattedItems);


Simple and clean.

Get Service Instance Anywhere in Laravel

Inject Service Instance by Constructor


Code Example:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MyController extends Controller
{
    protected $client;

    public function __construct(Client $client)
    {
        $this->client = $client;
    }

    public function store(Request $request)
    {
        $response = $this->client->message()->send([
            'text' => $request->input('text'),
        ]);
    }
}

Tuesday, June 5, 2018

PHP - remove all except letters, numbers, dashes and blanks

 
PHP - remove all except letters, numbers, dashes and blanks for various cases, like cleaning user input for DB storage, further execution and making it safe.


It's very easy to remove special characters.

$search = preg_replace('/[^a-zA-Z0-9  -]/', '', $search);


Also, if you would like to check if search string contains some characters, it's like this:

 if (preg_match('/[^a-zA-Z0-9 -]/', $search)) {
 echo 'invalid';
} else {
 echo 'valid';
}

Or in opposite case, to chech only for special characters:

preg_match('/[$&+,:;=?@#|\<>^*()%!]/', $userInput)

Wednesday, February 28, 2018

Image Meta Data&EXIF Removal


Image meta data can contain some information which image uploader wouldn't like to share with broad audience. Luckily, there is a great tool which strips all meta data and EXIF data. It works with JPG and PNG image formats.

There are a lot of tools claiming to clean meta data from image, but this one called 
JPEG & PNG Stripper works just awesome! It provides super easy and efficient meta removal.

Need bulk image cleaning? It's a piece of cake with this software. Just select your images and drag&drop in the application window. Plus, you'll get old school software feeling, back in a days when software did it's purpose, nothing more or less.




JPEG & PNG Stripper link