Skip to content

Instantly share code, notes, and snippets.

View khanzadimahdi's full-sized avatar
🤔
Don't ration passion!

mahdikhanzadi khanzadimahdi

🤔
Don't ration passion!
View GitHub Profile
@khanzadimahdi
khanzadimahdi / countries.php
Created March 9, 2020 06:10
All countries name, abbrev name, phone code and a pattern to match phone codes
<?php
$countryCodes = [
[
'abbr' => 'AD',
'name' => 'ANDORRA',
'code' => '376',
'pattern' => ''
],
[
@khanzadimahdi
khanzadimahdi / regex-match-utf8-hashtags.php
Created January 26, 2020 06:16
regex to match utf8 hashtags (similar to Instagram and Facebook)
/**
* @see https://regexr.com/4suqt
* @see https://regex101.com/r/4SAxik/1
* @see https://www.regexpal.com/?fam=113956
**/
$regex = "(?:#)([\p{L}\p{N}_](?:(?:[\p{L}\p{N}_]|(?:\.(?!\.))){0,28}(?:[\p{L}\p{N}_]))?)";
$text = "here is a #hashtag.tail and #another_hashtag #هشتگ #__hehe #123 this is a test.";
@khanzadimahdi
khanzadimahdi / wysiwyg-standard.json
Created December 19, 2019 09:51
a JSON standard for WYSIWYG editor
{
"version": "1.0",
"time": "12398421321",
"blocks": [
{
"type": "p",
"data": {
"text": "mahdi khanzadi"
},
"styles": {
@khanzadimahdi
khanzadimahdi / array_splice_assoc.php
Created August 25, 2019 07:31
php array_splice_assoc (array_splice associative)
<?php
/**
PHP array_splice doesn't use associative replacement keys, so i wrote the below method to do that.
*/
function array_splice_assoc(array &$original, int $offset, int $length = 0, $replacement = null) {
$slice = array_slice($original, 0, $offset, true);
if (!is_null($replacement)) {
// cast to array
@khanzadimahdi
khanzadimahdi / data-uri-regex-rfc2397.md
Last active December 1, 2024 19:17
regex pattern base64 data uri according to RFC 2397

pattern:

^data:((?:\w+\/(?:(?!;).)+)?)((?:;[\w\W]*?[^;])*),(.+)$

test this pattern on regexr: https://regexr.com/4inht

regex pattern to match RFC 2397 data URL

@khanzadimahdi
khanzadimahdi / string-limit.js
Created April 23, 2019 05:46
limit strings length in pure javascript
// add some prototypes
String.prototype.limit = function(length = 150, tail = '...') {
return this.substr(0, length) + (this.length > length ? tail : '');
};
// how to use:
'this is an string'.limit(5);
// or it can be like the below
let name = 'this is my name';
@khanzadimahdi
khanzadimahdi / laravel-on-delete.js
Last active February 4, 2019 06:51
laravel on delete request ask yes-no question
//use jquery and sweetAlert2
$(document).ready(function() {
$("form input[name~=_method][value~=DELETE]").closest('form').submit(function(event) {
event.preventDefault();
var form = $(this); //wrap this in jQuery
swal({
title: "are you sure?",
text: "do you realy want to delete this item?",
type: "warning",
showCancelButton: true,
@khanzadimahdi
khanzadimahdi / download-file-with-server.php
Last active January 23, 2019 20:38
download file in server using php
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
set_time_limit(0);
function download ($file_source, $file_target) {
echo 'starting';
$rh = fopen($file_source, 'rb');
$wh = fopen($file_target, 'w+b');
if (!$rh || !$wh) {
@khanzadimahdi
khanzadimahdi / fontwawesome-icons-4.7.json
Created September 25, 2018 17:09
fontawesome 4.7 icons list json
{
"icons": [
"fa fa-glass",
"fa fa-music",
"fa fa-search",
"fa fa-envelope-o",
"fa fa-heart",
"fa fa-star",
"fa fa-star-o",
"fa fa-user",
@khanzadimahdi
khanzadimahdi / laravel-multiple-upload.php
Last active January 23, 2019 20:19
laravel upload helper (upload single or multiple files + retrieve uploaded file's info)
<?php
if(!function_exists('fileUpload')){
/**
* upload files (single and multiple)
*
* @param \Illuminate\Http\Request $request
* @param $inputName
* @param string $disk
* @return array|mixed