ngfw / recipe
一组有用的PHP函数
1.2.4
2018-05-01 22:07 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: 5.7.19
README
PHP函数集合
目录
- 🚀 快速开始
- 网站图标
- 二维码
- 文件扩展名
- Gravatar
- 创建链接标签
- 验证电子邮件地址
- 验证URL
- RSS阅读器
- 对象转数组
- 数组转对象
- 数组转字符串
- 十六进制转RGB
- RGB转十六进制
- 颜色名称转十六进制
- 生成随机密码
- 简单编码
- 简单解码
- 生成服务器特定哈希
- 检测HTTPS
- 检测AJAX
- 检查数字是否为奇数
- 检查数字是否为偶数
- 获取当前URL
- 获取客户端IP
- 检测移动设备
- 获取浏览器
- 获取客户端位置
- 数字转文字
- 秒转文本
- 分钟转文本
- 小时转文本
- 缩短字符串
- CURL
- 缩短URL
- 获取Alexa排名
- 获取Tiny URL
- 从谷歌获取关键词建议
- WIKI搜索
- 通知
- 自动嵌入
- 使链接可点击
- 🔧 调试
- 获取引用者
- 压缩页面
- 序数
- 一个月中的天数
- pr
- 字节转人类可读大小
快速开始
在您的终端中运行
composer require ngfw/recipe
创建新文件并开始使用食谱
<?php require "vendor/autoload.php"; use ngfw\Recipe as Recipe; $suggestion = Recipe::getKeywordSuggestionsFromGoogle("home"); print_r($suggestion); // //Array //( // [0] => home depot // [1] => home goods // [2] => home depot near me // [3] => homes for sale // [4] => homeaway // [5] => homes for rent // [6] => home advisor // [7] => home depot credit card // [8] => home depot coupons // [9] => homeland //)
网站图标
获取远程网站网站图标
$favIcon = Recipe::getFavicon("http://youtube.com/"); echo $favIcon; // outputs: <img src="https://www.google.com/s2/favicons?domain=youtube.com/" />
使用HTML属性获取远程网站网站图标
$favIcon = Recipe::getFavicon( "http://youtube.com/", array( "class" => "favImg" ) ); echo $favIcon; //outputs: <img src="https://www.google.com/s2/favicons?domain=youtube.com/" class="favImg" />
二维码
生成二维码
$QRcode = Recipe::getQRcode("ngfw Recipe"); echo $QRcode; //outputs: <img src="http://chart.apis.google.com/chart?chs=150x150&cht=qr&chl=ngfw+Recipe" />
生成二维码并添加HTML属性
$QRcode = Recipe::getQRcode( "ngfw Recipe", $width = 350, $height = 350, $attributes = array( "class" => "QRCode" ) ); echo $QRcode; // outputs: <img src="http://chart.apis.google.com/chart?chs=350x350&cht=qr&chl=ngfw+Recipe" class="QRCode" />
文件扩展名
$ext = Recipe::getFileExtension(__FILE__); // replace '__FILE__' with your filename echo $ext; //outputs: php
Gravatar
获取Gravatar
$Gravatar = Recipe::getGravatar("gejadze@gmail.com"); echo $Gravatar; // outputs: <img src="http://www.gravatar.com/avatar.php?gravatar_id=9d9d478c3b65d4046a84cf84b4c8bf46&default=mm&size=80&rating=g" width="80px" height="80px" />
使用HTML属性获取Gravatar
$Gravatar = Recipe::getGravatar( "gejadze@gmail.com", $size = 200, $default = 'monsterid', $rating = 'x', $attributes = array( "class" => "Gravatar" ) ); echo $Gravatar; //Outputs: <img src="http://www.gravatar.com/avatar.php?gravatar_id=9d9d478c3b65d4046a84cf84b4c8bf46&default=monsterid&size=200&rating=x" width="200px" height="200px" class="Gravatar" />'
创建链接标签
简单链接
$linkTags = Recipe::createLinkTag("google.com"); echo $linkTags; //outputs: <a href="google.com">google.com</a>
带标题的链接
$linkTags = Recipe::createLinkTag("google.com", "Visit Google"); echo $linkTags; //outputs: <a href="google.com" title="Visit Google" >Visit Google</a>
带标题和HTML属性的链接
$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>
验证电子邮件地址
$isValid = Recipe::validateEmail("user@gmail.com"); var_dump($isValid); // outputs: true (bool)
检查临时电子邮件地址
$isValid = Recipe::validateEmail('user@fakeinbox.com', $tempEmailAllowed = false); var_dump($isValid); // outputs: false (bool)
验证URL
$isValid = Recipe::validateURL("http://github.com/"); var_dump($isValid); // outputs: true (bool)
RSS阅读器
$rssArray = Recipe::rssReader("https://github.com/ngfw/Recipe/commits/master.atom"); var_dump($rssArray); // Outputs feed as an array
对象转数组
$obj = new stdClass; $obj->foo = 'bar'; $obj->baz = 'qux'; $array = Recipe::objectToArray($obj); var_dump($array); // outputs: // array(2) { // ["foo"]=> // string(3) "bar" // ["baz"]=> // string(3) "qux" // }
数组转对象
$array = array( "foo" => "bar", "baz" => "qux", ); $obj = Recipe::arrayToObject($array); // outputs: // object(stdClass)#15 (2) { // ["foo"]=> // string(3) "bar" // ["baz"]=> // string(3) "qux" // }
数组转字符串
$array = array( "foo" => "bar", "baz" => "qux", ); $string = Recipe::arrayToString($array); echo $string; // outputs: foo="bar" baz="qux"
十六进制转RGB
$rgb = Recipe::hex2rgb("#FFF"); echo $rgb; // outputs: rgb(255, 255, 255)
RGB转十六进制
$hex = Recipe::rgb2hex("rgb(123,123,123)"); // outputs: #7b7b7b
颜色名称转十六进制
$hex = Recipe::colorNameToHex('red'); // outputs: #FF0000
生成随机密码
$randomPass = Recipe::generateRandomPassword(10); echo $randomPass; // outputs: 10 random character string
简单编码
$encodedString = Recipe::simpleEncode("php recipe"); echo $encodedString; // outputs: qcnVhqjKxpuilw==
简单解码
$decodedString = Recipe::simpleDecode("qcnVhqjKxpuilw=="); echo $decodedString; // outputs: php recipe
生成服务器特定哈希
$serverHash = Recipe::generateServerSpecificHash(); echo $serverHash; // outputs: d41d8cd98f00b204e9800998ecf8427e
检测HTTPS
此方法检查 $_SERVER['HTTPS']
$isHttps = Recipe::isHttps(); var_dump($isHttps); // outputs: bool
检测AJAX
此方法检查 $_SERVER['HTTP_X_REQUESTED_WITH']
$isAjax = Recipe::isAjax(); var_dump($isAjax); // outputs: bool
检查数字是否为奇数
$isNumberOdd = Recipe::isNumberOdd(5); // outputs: bool
检查数字是否为偶数
$isNumberEven = Recipe::isNumberEven(8); var_dump($isNumberEven); // outputs: bool
获取当前URL
$currentURL = Recipe::getCurrentURL(); var_dump($currentURL); // outputs: current Request URL
获取客户端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
检测移动设备
$isMobile = Recipe::isMobile(); var_dump($isMobile); // outputs: true or false
获取浏览器
$Browser = Recipe::getBrowser(); echo $Browser // outputs: Browser Details
获取客户端位置
$user_location = Recipe::getClientLocation(); echo $user_location; // outputs: Users Location
数字转文字
$number = "864210"; $number_in_words = Recipe::numberToWord($number); echo $number_in_words; // outputs: eight hundred and sixty-four thousand, two hundred and ten
秒转文本
$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 = 60 * 24 * 2; $duration = Recipe::minutesToText($minutes); echo $duration; // outputs: 2 days // Recipe::minutesToText($minutes, $returnAsWords = true); // will return: two days
小时转文本
$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
缩短字符串
$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
简单GET示例
$data = Recipe::curl("https://api.ipify.org"); var_dump($data); // outputs: Curl'ed Data
POST示例
$CurlPOST = Recipe::curl("http://jsonplaceholder.typicode.com/posts", $method = "POST", $data = array( "title" => 'foo', "body" => 'bar', "userId" => 1, ));
自定义头
$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 //}
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', ) );
展开短URL
$shortURL = "https://goo.gl/rvDnMX"; $expandedURL = Recipe::expandShortUrl($shortURL); echo $expendedURL; // outputs: https://github.com/ngfw/Recipe
获取Alexa排名
$AlexaRank = Recipe::getAlexaRank("github.com"); echo $AlexaRank; // outputs: Current alexa ranking as position number (example: 52)
缩短URL
$TinyUrl = Recipe::getTinyUrl("https://github.com/ngfw/Recipe"); echo $TinyUrl; // outputs: http://tinyurl.com/h2nchjh
从谷歌获取关键词建议
$suggestions = Recipe::getKeywordSuggestionsFromGoogle("Tbilisi, Georgia"); var_dump($suggestions); // outputs: //array(10) { // [0]=> // string(15) "tbilisi georgia" // [1]=> // string(22) "tbilisi georgia hotels" // [2]=> // string(19) "tbilisi georgia map" // [3]=> // string(20) "tbilisi georgia time" // [4]=> // string(23) "tbilisi georgia airport" // [5]=> // string(23) "tbilisi georgia weather" // [6]=> // string(24) "tbilisi georgia language" // [7]=> // string(24) "tbilisi georgia zip code" // [8]=> // string(20) "tbilisi georgia news" // [9]=> // string(28) "tbilisi georgia airport code" //}
WIKI搜索
$wiki = Recipe::wikiSearch("Tbilisi"); var_dump($wiki); // outputs: data from wikipedia
通知
$notification = Recipe::notification("Test Successful"); echo $notification; // outputs: <div style="display: block;padding: 0.5em;border: solid 1px;border-radius: 0.125em;margin-bottom: 1em; border-color: #a6d9f2;color: #0a5276;background-color: #e7f6fd;" role="alert">Test Successful</div> // NOTE: possible notifications types: success, warning, error and info // Type is passed as a second parameter
自动嵌入
$string = "Checkout Solomun, Boiler Room at https://www.youtube.com/watch?v=bk6Xst6euQk"; echo Recipe::autoEmbed($string); // outputs: // Checkout Solomun, Boiler Room at<iframe width="560" height="315" src="https://www.youtube.com/embed/bk6Xst6euQk?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe> // 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
使链接可点击
$string = "Check PHP Recipes on https://github.com/ngfw/Recipe"; $clickable = Recipe::makeClickableLinks($string); echo $clickable; // outputs: // Check PHP Recipes on <a href="https://github.com/ngfw/Recipe" >https://github.com/ngfw/Recipe</a>
调试
var_dump() 的替代品
$string = "Test me"; Recipe::debug($string);
获取引用者
获取引用者页面(最后访问的页面)
$referrer = Recipe::getReferer(); echo $referer ; // outputs an url (http://mywebsite.com/page1)
序数
for($i=1;$i<=10;$i++){ echo Recipe::ordinal($i); echo ' '; } // outputs 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th
一个月中的天数
$numDays = Recipe::numberOfDaysInMonth(2, 2012); echo $numDays; // outputs: 29
压缩页面
compressPage() 方法将在PHP关闭时注册新函数,从输出中删除空白并尝试gzip它。
<?php require "vendor/autoload.php"; Recipe::compressPage(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>HTML Page Title</title> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> Hello Friend, </body> </html>
将输出
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>HTML Page Title</title><meta name="description" content=""><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body> Hello Friend,</body></html>
PR
Recipe::pr( array("he","ll","oo") );
将输出
<pre>Array ( [0] => he [1] => ll [2] => oo ) </pre>
字节转人类可读大小
Recipe::bytesToHumanReadableSize( "17179869184" );
将输出
16 GB