10 のよく使用される PHP スニペット_PHP チュートリアル

WBOY
リリース: 2016-07-13 10:37:21
オリジナル
1225 人が閲覧しました

1.経度と緯度の間の距離

'lat'=> 40.758224,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 function getDistanceBetweenPointsNew($latitude1, $経度1、$緯度2、$経度2) { <code class="js space"> <code class="js spaces"> $theta = $経度1 - $経度2; <code class="js space"> <code class="js spaces"> $マイル = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta))); <code class="js space"> <code class="js spaces"> $マイル = acos($マイル); <code class="js space"> <code class="js spaces"> $マイル = rad2deg($マイル); <code class="js space"> <code class="js spaces"> $マイル = $マイル * 60 * 1.1515; <code class="js space"> <code class="js spaces"> $feet = $マイル * 5280; <code class="js space"> <code class="js spaces"> $yards = $フィート / 3; <code class="js space"> <code class="js spaces"> $キロ = $マイル * 1.609344; <code class="js space"> <code class="js spaces"> $meters = $キロ * 1000; <code class="js スペース"> <code class="js spaces"> return compact('miles','feet','yards','kilometers','meters');return<code class="js plain">} $point1 = array('lat'compact(<code class="js string">'マイル''long' => -73.964367);,$point2 = array('lat' 'フィート'<code class="js plain">,'long' => -73.917404);'ヤード'<code class="js plain">$distance = getDistanceBetweenPointsNew($point1['lat','キロメートル'<code class="js string">'long'], $point2['lat','メートル'<code class="js string">'long']);foreach ($distance as $unit => $value) {); } ': '.number_format($value,4).'$point1 = array(';'lat'<code class="js plain">} The example returns the following:=> 40.770623, miles: 2.6025'long'<code class="js plain">feet: 13,741.4350 yards: 4,580.4783=> -73.964367);kilometers: 4.1884 meters: 4,188.3894$point2 = array(
'long'🎜 🎜=> -73.917404);🎜 🎜$ distance = getDistanceBetweenPointsNew($point1[🎜'lat'🎜🎜], $point1[🎜'long'🎜🎜], $point2[🎜'lat'🎜🎜], $point2[🎜'long'🎜🎜]);🎜 🎜foreach ($ distance as $unit => $value) {🎜 <code class="js space"> 🎜🎜echo $unit.🎜': '🎜🎜.number_format($value,4).🎜'🎜 '🎜🎜;🎜 🎜}🎜 🎜この例は次を返します:🎜 🎜マイル: 2.6025🎜 🎜フィート: 13,741.4350🎜 🎜ヤード: 4,580.4783🎜 🎜キロ: 4.1884🎜 🎜メートル: 4,188.3894🎜 🎜 🎜 🎜 🎜
2.完善cURL機能 <code class="js スペース"> if
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 function xcurl($url,$ref=null,$post=array(),$ua=「モジラ/5.0」 (X11; Linux x86_64; rv:2.2a1pre) Gecko/20110324 Firefox/4.2a1pre",$print=false,$print=<code class="js キーワード">false $ch = curl_init();) { curl_setopt($ch, CURLOPT_AUTOREFERER, true); <code class="js space"> <code class="js spaces"> if$ch =curl_init(); <code class="js space"> <code class="js spaces"> curl_setopt($ch, CURLOPT_REFERER, $ref); }curl_setopt($ch, CURLOPT_AUTOREFERER, <code class="js spaces"> curl_setopt($ch, CURLOPT_URL, $url);true<code class="js spaces"> curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); <code class="js スペース"> <code class="js spaces"> ifif<code class="js plain">(!empty($ref)) { curl_setopt($ch, CURLOPT_USERAGENT, $ua); <code class="js space"> <code class="js spaces"> } ifcurl_setopt($ch, CURLOPT_REFERER, $ref); <code class="js space"> curl_setopt($ch, CURLOPT_POST, 1);} curl_setopt($ch, CURLOPT_POSTFIELDS, $post); <code class="js space"> <code class="js spaces"> } $output = curl_exec($ch);curl_setopt($ch, CURLOPT_URL, $url); curl_close($ch); <code class="js space"> <code class="js spaces"> ifcurl_setopt($ch, CURLOPT_HEADER, 0); <code class="js space"> <code class="js spaces"> print($output); } else {curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); return $output; <code class="js space"> <code class="js spaces"> }}curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
(!empty($ua)) {🎜 <code class="js space"> 🎜🎜curl_setopt($ch, CURLOPT_USERAGENT, $ua);🎜 <code class="js space"> 🎜🎜}🎜 <code class="js スペース"> 🎜if🎜🎜(count($post) > 0){🎜 <code class="js space"> 🎜🎜curl_setopt($ch, CURLOPT_POST, 1);🎜 <code class="js space"> 🎜🎜curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 🎜 <code class="js space"> 🎜🎜}🎜 <code class="js space"> 🎜🎜$output =curl_exec($ch);🎜 <code class="js space"> 🎜🎜curl_close($ch);🎜 <code class="js スペース"> 🎜if🎜🎜($print) {🎜 <code class="js space"> 🎜🎜print($output);🎜 <code class="js スペース"> 🎜🎜} 🎜else🎜 🎜{🎜 <code class="js スペース"> 🎜return🎜 🎜$output;🎜 <code class="js space"> 🎜🎜}🎜 🎜}🎜 🎜 🎜 🎜 🎜


3.清理用户输入 $val) { <code class="js スペース">
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ]*?>.*?@si', // Strip out javascript '@]*?>@si'、 // HTMLタグを取り除きます <code class="js space"> <code class="js spaces"> '@]*?>.*?@siU' '@@@]*?>.*?@siU', // スタイルタグを適切に削除します ); <code class="js space"> <code class="js spaces"> $output = preg_replace($search, '' return $output;@@ }?>' // 複数行のコメントを削除します$val) { <code class="js space"> <code class="js spaces"> $output[$var); <code class="js space"> <code class="js spaces"> } }$output = preg_replace($search, ' else { if', $input); <code class="js スペース"> return<code class="js spaces"> $input = stripslashes($input); <code class="js spaces"> }$output; $input = cleanInput($input); <code class="js space"> <code class="js spaces"> $output = mysql_real_escape_string($input); }} return $output; }?>?>
$output[$🎜var🎜🎜] = サニタイズ($val);🎜 <code class="js space"> 🎜🎜}🎜 <code class="js space"> 🎜🎜}🎜 <code class="js スペース"> 🎜else🎜 🎜{🎜 <code class="js スペース"> 🎜if🎜 🎜(get_magic_quotes_gpc()) {🎜 <code class="js space"> 🎜🎜$input =tripslashes($input);🎜 <code class="js space"> 🎜🎜}🎜 <code class="js space"> 🎜🎜$input = cleanInput($input);🎜 <code class="js space"> 🎜🎜$output = mysql_real_escape_string($input);🎜 <code class="js space"> 🎜🎜}🎜 <code class="js スペース"> 🎜return🎜 🎜$output;🎜 🎜}🎜 🎜?>🎜 🎜 🎜 🎜 🎜


4.通过IP(城市、国家)检测地理的位置 ); <code class="js space">
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 function detect_city($ip) { <code class="js スペース"> <code class="js spaces"> $default = $default<code class="js plain">; <code class="js spaces"> if= 'ハリウッド、 キャ<code class="js string">'127.0.0.1'; <code class="js スペース"> <code class="js string">'localhost') $ip = '8.8.8.8'if<code class="js plain"> (!is_string($ip) || strlen($ip) 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)''127.0.0.1' 'http://ipinfodb.com/ip_locator.php?ip='|| $ip == 'ローカルホスト' CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => 1,) $ip = <code class="js spaces"> CURLOPT_USERAGENT => $curlopt_useragent, CURLOPT_URL => $url,'8.8.8.8' CURLOPT_TIMEOUT => 1, CURLOPT_REFERER => 'http://'; $curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)''HTTP_HOST'], );; $url = <code class="js spaces"> curl_setopt_array($ch, $curl_opt); $content = curl_exec($ch);'http://ipinfodb.com/ip_locator.php?ip=' if 。 urlencode($ip); $ch =curl_init(); $curl_opt = array( CURLOPT_FOLLOWLOCATION => 1, <code class="js space"> <code class="js spaces"> $curl_info = curl_getinfo($ch); }CURLOPT_HEADER => 0, curl_close($ch); <code class="js space"> <code class="js spaces"> ifCURLOPT_RETURNTRANSFER => 1, <code class="js space"> <code class="js string">'{ CURLOPT_USERAGENT => $curlopt_useragent,City : ([^ <code class="js space"> }i', $content, $regs) ) { $city = $regs[1]; } ifCURLOPT_URL => $url, <code class="js space"> <code class="js string">'{ CURLOPT_TIMEOUT => 1,State/Province : ([^ <code class="js space"> }i', $content, $regs) ) { $state = $regs[1]; } ifCURLOPT_REFERER => '' && $state!='''http://' 。 $_SERVER[', ' . $state; return'HTTP_HOST'else{ return $default], <code class="js space">
curl_setopt_array($ch, $curl_opt);🎜 <code class="js space"> 🎜🎜$content =curl_exec($ch);🎜 <code class="js スペース"> 🎜if🎜 🎜(!is_null($curl_info)) {🎜 <code class="js space"> 🎜🎜$curl_info =curl_getinfo($ch);🎜 <code class="js space"> 🎜🎜}🎜 <code class="js space"> 🎜🎜curl_close($ch);🎜 <code class="js スペース"> 🎜if🎜 🎜( preg_match(🎜🎜'{🎜 <code class="js スペース"> 🎜 🎜都市: ([^if🎜 🎜( preg_match(🎜🎜'{🎜 <code class="js スペース"> 🎜 🎜州/県: ([^if🎜🎜( $city!=🎜🎜''🎜 🎜&& $state!=🎜🎜''🎜 🎜){ $location = $city 。 🎜🎜』、『🎜🎜。 $state; 🎜return🎜 🎜$location; }🎜else🎜🎜{ 🎜return🎜 🎜$🎜default🎜🎜; } }🎜 🎜 🎜 🎜 🎜


5.パスワードの強度を設定する ));エコー
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 100){ $strength = 100; } return $strength; } var_dump(password_strength(「正しい馬電池」 ステープル"));));echo " ";エコーvar_dump(password_strength("Super Monkey Ball"));echo ""; var_dump(password_strength("Tr0ub4dor&3"));echo "";;var_dump(password_strength("abc123"));echo "";「スーパーモンキーボール」var_dump(password_strength("sweet"));



;」 「Tr0ub4dor&3」));
functionエコーdefault='en'){ if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'; $langs=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);「abc123」 //start going through each one foreach ($langs as $value){)); $choice=substr($value,0,2);エコー if return $choice; }; } }「甘い」 return $default;}));



6. ブラウザ言語を検出し、利用可能な $availableLanguages のみを配列 ('en'、'de'、'es') として提供します 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 'en'){
function function get_client_ language($availableLanguages, $ $contents=file_get_contents($file);default $base64=base64_encode($contents); echo "data:$mime;base64,$base64";=}
<code class="js スペース"> 🎜 <code class="js スペース"> 🎜if🎜 🎜(isset($_SERVER[🎜🎜'HTTP_ACCEPT_LANGUAGE'🎜🎜])) {🎜 <code class="js スペース"> 🎜 <code class="js space"> 🎜🎜$langs=explode(🎜🎜','🎜🎜,$_SERVER[🎜🎜'HTTP_ACCEPT_LANGUAGE'🎜🎜]);🎜 <code class="js space"> 🎜//それぞれの説明を開始します🎜 <code class="js space"> 🎜🎜foreach ($langs as $value){🎜 <code class="js スペース"> 🎜 <code class="js space"> 🎜🎜$choice=substr($value,0,2);🎜 <code class="js スペース"> 🎜if🎜🎜(in_array($choice, $availableLanguages)){🎜 <code class="js スペース"> 🎜return🎜 🎜$choice;🎜 <code class="js スペース"> 🎜 <code class="js space"> 🎜🎜 <code class="js スペース"> 🎜 <code class="js space"> 🎜🎜 🎜 <code class="js スペース"> 🎜return🎜 🎜$🎜default🎜🎜;🎜 🎜 🎜 🎜 🎜 🎜 🎜 🎜 🎜 🎜7. データURLの作成🎜 🎜 🎜 🎜 🎜 1 2 3 4 5 🎜 🎜 function🎜 🎜data_uri($file, $mime) {🎜 <code class="js space"> 🎜🎜$contents=file_get_contents($file);🎜 <code class="js space"> 🎜🎜$base64=base64_encode($contents);🎜 <code class="js space"> 🎜🎜echo 🎜🎜"data:$mime;base64,$base64"🎜🎜;🎜 🎜 🎜 🎜 🎜 🎜


8. より分かりやすいページタイトルのSEO URLを作成します 入力例: $title = “この foo のバーはロッキン クールです!”; echo makeeoname($title); //RETURNS: //this-foos-bar-is-rockin-cool; 、 strto lower(str_replace(' '
1 2 3 function make_seo_name($title) { <code class="js スペース"> <code class="js spaces"> return preg_replace('/[^a-z0-9_-]/i', ''return preg_replace(' ', '-''/[^a-z0-9_-]/i'}''

,
'-'
, トリム($title))));} <code class="js space"> // と md5...
// f(ucking) u(ncrackable) e(ncryption) function by BlackHatDBL (www.netforme.net) function 9. 究極の暗号化機能 for 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // Encode with base64... $hash=base64_encode($hash);// BlackHatDBL (www.netforme.net) による f(ucking) u(ncrackable) e(ncryption) 関数 // and md5... $hash=md5($hash); <code class="js spaces"> // sha1...fue($hash,$times) { $hash=sha1($hash); <code class="js space"> <code class="js spaces"> // sha256... (one more) $hash=hash("sha256"// ユーザーの数だけ暗号化を実行します 欲しい <code class="js スペース"> for // sha512 $hash=hash("sha512"($i=$times;$i>0;$i--) { <code class="js space"> }// Base64 でエンコードします... // Finaly, when done, return the value <code class="js space"> <code class="js spaces"> return $hash;}$hash=base64_encode($hash);
<code class="js space"> 🎜🎜$hash=md5($hash);🎜 <code class="js space"> 🎜🎜// sha1...🎜 <code class="js space"> 🎜🎜$hash=sha1($hash);🎜 <code class="js space"> 🎜🎜// sha256... (もう 1 つ)🎜 <code class="js space"> 🎜🎜$hash=hash(🎜"sha256"🎜🎜, $ハッシュ);🎜 <code class="js space"> 🎜🎜// sha512🎜 <code class="js space"> 🎜🎜$hash=hash(🎜"sha512"🎜🎜, $ハッシュ);🎜 <code class="js space"> 🎜🎜}🎜 <code class="js space"> 🎜🎜// 最後に、完了したら値を返します🎜 <code class="js スペース"> 🎜return🎜 🎜$hash;🎜 🎜}🎜 🎜 🎜 🎜 🎜


10a.Tweeter Feed Runner—任意のtwitter名を使用して、任意のページにユーザーのリソースを追加できます。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <code class="js space"> $
pversion; } <code class="js spaces"> public functionloadTimeline($user, $max = 20){ <code class="js space"> $this$this'statuses/user_timeline.xml?screen_name='.$user.'&count='.$max;->twitURL .= <code class="js spaces"> $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->twitURL);'statuses/user_timeline.xml?screen_name=' curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $this.$user.'&count=' return $this; }.$max; public function getTweets(){ <code class="js space"> <code class="js spaces"> $this$ch =curl_init(); <code class="js space"> this->getTimelineArray(); $tweets = array();curl_setopt($ch, CURLOPT_URL, $ foreach($thisthis->twitURL); $tweets[$status->created_at->__toString()] = $status->text->__toString(); <code class="js space"> <code class="js spaces"> } return $tweets;curl_setopt($ch, CURLOPT_RETURNTRANSFER, <code class="js spaces"> }true<code class="js spaces"> public function getTimelineArray(){ return simplexml_load_string($this->xml);); } <code class="js スペース"> <code class="js spaces"> public function formatTweet($tweet){ $tweet = preg_replace($this,""->xml =curl_exec($ch); <code class="js スペース"> 戻る $tweet = preg_replace("/#(.+?)(h|W|$)/", "#" $これ $tweet = preg_replace("/@(.+?)(h|W|$)/", "@"; return $tweet; } }getTweets(){
this🎜🎜->twitterArr = $🎜this🎜🎜->getTimelineArray();🎜 <code class="js space"> 🎜🎜$tweets = array();🎜 <code class="js space"> 🎜🎜foreach($🎜this🎜🎜->twitterArr->status $status として){🎜 <code class="js space"> 🎜🎜$tweets[$status->created_at->__toString()] = $status->text->__toString();🎜 <code class="js space"> 🎜🎜}🎜 <code class="js スペース"> 🎜return🎜 🎜$tweets;🎜 🎜 🎜 🎜getTimelineArray(){🎜 <code class="js スペース"> 🎜return🎜 🎜simplexml_load_string($🎜this🎜🎜->xml);🎜 🎜 🎜 🎜フォーマットツイート($ツイート){🎜 <code class="js space"> 🎜🎜$tweet = preg_replace(🎜🎜"/(http(.+?))( |$)/"🎜🎜,🎜🎜"$1$3"🎜🎜, $ツイート);🎜 <code class="js space"> 🎜🎜$tweet = preg_replace(🎜🎜"/#(.+?)(h|W|$)/"🎜🎜, 🎜🎜"#$1$2"🎜🎜, $ツイート);🎜 <code class="js space"> 🎜🎜$tweet = preg_replace(🎜🎜"/@(.+?)(h|W|$)/"🎜🎜, 🎜🎜"@$1$2"🎜🎜, $ツイート);🎜 <code class="js スペース"> 🎜return🎜 🎜$tweet;🎜 🎜 🎜}🎜 🎜 🎜 🎜 🎜10b. ツイーター フィード ランナー - example.php などのテーマ内のファイルを作成するために使用されます。 ";}
1 2 3 4 loadTimeline("phpsnips")->getTweets(); foreach($feed as $time => $message){ echo "
".$twitter->formatTweet($message)..$twitter->formatTweet($message)."
At: "
.$time..$time."
"
;}"

http://www.bkjia.com/PHPjc/735898.htmlwww.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/735898.html
技術記事 1. 経度と緯度の間の距離を求める 12345678910111213141516171819202122232425262728 function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2)...
🎜🎜 🎜🎜
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!