Recipe là tập hợp các hàm PHP tiện dụng, mã nguồn tại Recipe

Cài đặt Recipe

Dùng composer cài đặt bằng lệnh:

composer require ngfw/recipe

Hoặc tải về từ composer github.com/ngfw/Recipe

Các hàm Recipe

Dùng composer cài đặt bằng lệnh:

Favicon - lấy favicon của một website

$favIcon = Recipe::getFavicon("http://youtube.com/");
echo $favIcon;

// outputs: <img src="https://www.google.com/s2/favicons?domain=youtube.com/"  />

QRcode - phát sinh mã QR

$QRcode = Recipe::getQRcode("ngfw Recipe");
echo $QRcode;

getFileExtension - lấy phần mở rộng của file

$ext = Recipe::getFileExtension(filename);

getGravatar - lấy avatar từ Gravatar

$Gravatar = Recipe::getGravatar("gejadze@gmail.com");

createLinkTag - Tạo liên kết (link)

Dạng link đơn giản

$linkTags = Recipe::createLinkTag("google.com");
echo $linkTags;   
//outputs: <a href="google.com">google.com</a>

Tạo link có title

$linkTags = Recipe::createLinkTag("google.com", "Visit Google");
echo $linkTags;   
//outputs: <a href="google.com" title="Visit Google" >Visit Google</a>

Tạo link có tiêu đề cùng các thuộc tính HTML khác

$linkTags = Recipe::createLinkTag("google.com", "Visit Google", array(
    "class" => "outgoingLink"
));
echo $linkTags;   
//outputs: <a href="google.com" title="Visit Google" class="outgoingLink">Visit Google</a>

validateEmail - Kiểm tra hợp lệ của email

$isValid = Recipe::validateEmail("user@gmail.com");

validateURL - kiểm tra hợp lệ của một URL

$isValid = Recipe::validateURL("http://github.com/");

Đọc Rss - trả về mảng là các mẩu tin RSS

$rssArray = Recipe::rssReader("https://github.com/ngfw/Recipe/commits/master.atom");

objectToArray - Chuyển một đối tượng thành mảng

$obj = new stdClass;
$obj->foo = 'bar';
$obj->baz = 'qux';
$array = Recipe::objectToArray($obj);
var_dump($array);

arrayToObject - Chuyển mạng thành đối tượng

$array = array(
    "foo" => "bar",
    "baz" => "qux",
);
$obj = Recipe::arrayToObject($array);

arrayToString - Chuyển mảng thành chuối 

$array = array(
    "foo" => "bar",
    "baz" => "qux",
);
$string = Recipe::arrayToString($array);
echo $string;
// outputs: foo="bar" baz="qux"

 

Chuyển mã màu dạng HEX thành RGB và ngược lại

$rgb = Recipe::hex2rgb("#FFF");
echo $rgb;
// outputs: rgb(255, 255, 255) 

$hex = Recipe::rgb2hex("rgb(123,123,123)");

// outputs: #7b7b7b

Phát sinh password có số ký tự chọn trước generateRandomPassword

$randomPass = Recipe::generateRandomPassword(10);
echo $randomPass; // chuỗi password dài 10 ký tự

Mã hóa đơn giản

$decodedString = Recipe::simpleDecode("qcnVhqjKxpuilw==");
echo $decodedString;

Detect HTTPS

This method checks for $_SERVER['HTTPS']

$isHttps = Recipe::isHttps();
var_dump($isHttps);
// outputs: bool


Detect AJAX

This method checks for $_SERVER['HTTP_X_REQUESTED_WITH']

$isAjax = Recipe::isAjax();
var_dump($isAjax);
// outputs: bool


Check if number is odd

$isNumberOdd = Recipe::isNumberOdd(5);
// outputs: bool


Check if number is even

$isNumberEven = Recipe::isNumberEven(8);
var_dump($isNumberEven);
// outputs: bool


Get Current URL

$currentURL = Recipe::getCurrentURL();
var_dump($currentURL);
// outputs: current Request URL


Get Client IP

$ClientsIP = Recipe::getClientIP();
echo $ClientsIP;
//OR
// Return Proxy IP if user is behind it
//$ClientsIP = Recipe::getClientIP("HTTP_CLIENT_IP"); //'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED', ...
// outputs: IP address


Detect Mobile

$isMobile = Recipe::isMobile();
var_dump($isMobile);
// outputs: true or false


Get Browser

$Browser = Recipe::getBrowser();
echo $Browser
// outputs: Browser Details


Get Client Location

$user_location = Recipe::getClientLocation();
echo $user_location;
// outputs: Users Location


Number To Word conversion

$number = "864210";
$number_in_words = Recipe::numberToWord($number);
echo $number_in_words;
// outputs: eight hundred and sixty-four thousand, two hundred and ten


Seconds To Text

$seconds = "864210";
$number_in_words = Recipe::secondsToText($seconds);
echo $number_in_words;
// outputs: 1 hour and 10 seconds
// Recipe::secondsToText($seconds, $returnAsWords = true);
// will return: one hour and ten seconds


Minutes To Text

$minutes  = 60 * 24 * 2;
$duration = Recipe::minutesToText($minutes);
echo $duration;
// outputs: 2 days
// Recipe::minutesToText($minutes, $returnAsWords = true);
// will return: two days


Hours To Text

$hours    = 4.2;
$duration = Recipe::hoursToText($hours);
echo $duration;
// outputs: 4 hours and 12 minutes
// Recipe::hoursToText($hours, $returnAsWords = true);
// will return: four hours and twelve minutes


Shorten String

$string        = "The quick brown fox jumps over the lazy dog";
$shortenString = Recipe::shortenString($string, 20);
// output: The quick brown f...
// Recipe::shortenString($string, 20, $addEllipsis = false);
// output: "The quick brown fox ", NOTE last space
// Recipe::shortenString($string, 20, $addEllipsis = false, $wordsafe = true);
// output: "The quick brown fox", NOTE, will not break in the middle of the word


CURL

Simple GET example:

$data = Recipe::curl("https://api.ipify.org");
var_dump($data);
// outputs: Curl'ed Data


POST Example:

$CurlPOST = Recipe::curl("http://jsonplaceholder.typicode.com/posts", $method = "POST", $data = array(
    "title"  => 'foo',
    "body"   => 'bar',
    "userId" => 1,
));


Custom Headers:

$curlWithHeaders = Recipe::curl("http://jsonplaceholder.typicode.com/posts", $method = "GET", $data = false, $header = array(
    "Accept" => "application/json",
), $returnInfo = true);
// NOTE $returnInfo argument
// Result will be returned as an array, $curlWithHeaders={
//  info => containing curl information, see curl_getinfo()
//  contents  => Data from URL
//}


Basic authentication with CURL:

$curlBasicAuth = Recipe::curl(
    "http://jsonplaceholder.typicode.com/posts",
    $method = "GET",
    $data = false,
    $header = false,
    $returnInfo = false,
    $auth = array(
       'username' => 'your_login',
       'password' => 'your_password',
    )
);


Expand Short URL

$shortURL = "https://goo.gl/rvDnMX";
$expandedURL = Recipe::expandShortUrl($shortURL);
echo $expendedURL;
// outputs: https://github.com/ngfw/Recipe


Get Alexa Rank

$AlexaRank = Recipe::getAlexaRank("github.com");
echo $AlexaRank;
// outputs: Current alexa ranking as position number (example: 52)


Get Google PageRank

$GoogleRank = Recipe::getGooglePageRank("github.com");
echo $GoogleRank;
// outputs: Current google page ranking
Shorten URL

$TinyUrl = Recipe::getTinyUrl("https://github.com/ngfw/Recipe");
echo $TinyUrl;
// outputs: http://tinyurl.com/h2nchjh


Get Keyword Suggestions From Google

$suggestions = Recipe::getKeywordSuggestionsFromGoogle("Tbilisi, Georgia");
var_dump($suggestions);

WIKI Search

$wiki = Recipe::wikiSearch("Tbilisi");
var_dump($wiki);
// outputs: data from wikipedia

Notification

$notification = Recipe::notification("Test Successful");
echo $notification;


Auto Embed

$string = "Checkout Solomun, Boiler Room at https://www.youtube.com/watch?v=bk6Xst6euQk";
echo Recipe::autoEmbed($string);
// supported providers are: youtube.com, blip.tv, vimeo.com, dailymotion.com, flickr.com, smugmug.com, hulu.com, revision3.com, wordpress.tv, funnyordie.com, soundcloud.com, slideshare.net and instagram.com


Make Clickable Links

$string = "Check PHP Recipes on https://github.com/ngfw/Recipe";
$clickable = Recipe::makeClickableLinks($string);
echo $clickable;


Get Referer

Get the referer page (last page visited)

$referrer = Recipe::getReferer();
echo $referer ;
// outputs an url (http://mywebsite.com/page1)

 
Number Of Days In Month

$numDays = Recipe::numberOfDaysInMonth(2, 2012);
echo $numDays;
// outputs: 29


Compress Page

The compressPage() method will register new function on PHP shutdown, remove white space from output and try to gZip it.

 

 


Đăng ký nhận bài viết mới