ホームページ  >  記事  >  php教程  >  必須の PHP コード サンプル 10 個

必須の PHP コード サンプル 10 個

WBOY
WBOYオリジナル
2016-06-21 08:46:271007ブラウズ

1. ブラックリストフィルタリング

<ol class="dp-j">
<li class="alt"><span><span>function is_spam($text, $file, $split = </span><span class="string">':'</span><span>, $regex = </span><span class="keyword">false</span><span>){ </span></span></li>
<li><span>    $handle = fopen($file, <span class="string">'rb'</span><span>); </span></span></li>
<li class="alt"><span>    $contents = fread($handle, filesize($file)); </span></li>
<li><span>    fclose($handle); </span></li>
<li class="alt"><span>    $lines = explode(<span class="string">"n"</span><span>, $contents); </span></span></li>
<li><span>$arr = array(); </span></li>
<li class="alt"><span>foreach($lines as $line){ </span></li>
<li><span>list($word, $count) = explode($split, $line); </span></li>
<li class="alt"><span><span class="keyword">if</span><span>($regex) </span></span></li>
<li><span>$arr[$word] = $count; </span></li>
<li class="alt"><span><span class="keyword">else</span><span> </span></span></li>
<li><span>$arr[preg_quote($word)] = $count; </span></li>
<li class="alt"><span>} </span></li>
<li><span>preg_match_all(<span class="string">"~"</span><span>.implode(</span><span class="string">''</span><span>, array_keys($arr)).</span><span class="string">"~"</span><span>, $text, $matches); </span></span></li>
<li class="alt"><span>$temp = array(); </span></li>
<li><span>foreach($matches[<span class="number">0</span><span>] as $match){ </span></span></li>
<li class="alt"><span><span class="keyword">if</span><span>(!in_array($match, $temp)){ </span></span></li>
<li><span>$temp[$match] = $temp[$match] + <span class="number">1</span><span>; </span></span></li>
<li class="alt"><span><span class="keyword">if</span><span>($temp[$match] >= $arr[$word]) </span></span></li>
<li><span><span class="keyword">return</span><span> </span><span class="keyword">true</span><span>; </span></span></li>
<li class="alt"><span>} </span></li>
<li><span>} </span></li>
<li class="alt"><span><span class="keyword">return</span><span> </span><span class="keyword">false</span><span>; </span></span></li>
<li><span>} </span></li>
<li class="alt"><span> </span></li>
<li><span>$file = <span class="string">'spam.txt'</span><span>; </span></span></li>
<li class="alt"><span>$str = <span class="string">'This string has cat, dog word'</span><span>; </span></span></li>
<li><span><span class="keyword">if</span><span>(is_spam($str, $file)) </span></span></li>
<li class="alt"><span>echo <span class="string">'this is spam'</span><span>; </span></span></li>
<li><span><span class="keyword">else</span><span> </span></span></li>
<li class="alt"><span>echo <span class="string">'this is not spam'</span><span>; </span></span></li>
<li><span> </span></li>
<li class="alt"><span>ab:<span class="number">3</span><span> </span></span></li>
<li><span>dog:<span class="number">3</span><span> </span></span></li>
<li class="alt"><span>cat:<span class="number">2</span><span> </span></span></li>
<li><span>monkey:<span class="number">2</span><span> </span></span></li>
</ol>

2. ランダムカラージェネレーター

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> randomColor() { </span></span></li>
<li><span>    <span class="vars">$str</span><span> = </span><span class="string">'#'</span><span>; </span></span></li>
<li class="alt"><span>    <span class="keyword">for</span><span>(</span><span class="vars">$i</span><span> = 0 ; </span><span class="vars">$i</span><span> < 6 ; </span><span class="vars">$i</span><span>++) { </span></span></li><li><span>        <span class="vars">$randNum</span><span> = rand(0 , 15); </span></span></li><li class="alt"><span>        <span class="keyword">switch</span><span> (</span><span class="vars">$randNum</span><span>) { </span></span></li><li><span>            <span class="keyword">case</span><span> 10: </span><span class="vars">$randNum</span><span> = </span><span class="string">'A'</span><span>; </span><span class="keyword">break</span><span>; </span></span></li><li class="alt"><span>            <span class="keyword">case</span><span> 11: </span><span class="vars">$randNum</span><span> = </span><span class="string">'B'</span><span>; </span><span class="keyword">break</span><span>; </span></span></li><li><span>            <span class="keyword">case</span><span> 12: </span><span class="vars">$randNum</span><span> = </span><span class="string">'C'</span><span>; </span><span class="keyword">break</span><span>; </span></span></li><li class="alt"><span>            <span class="keyword">case</span><span> 13: </span><span class="vars">$randNum</span><span> = </span><span class="string">'D'</span><span>; </span><span class="keyword">break</span><span>; </span></span></li><li><span>            <span class="keyword">case</span><span> 14: </span><span class="vars">$randNum</span><span> = </span><span class="string">'E'</span><span>; </span><span class="keyword">break</span><span>; </span></span></li><li class="alt"><span>            <span class="keyword">case</span><span> 15: </span><span class="vars">$randNum</span><span> = </span><span class="string">'F'</span><span>; </span><span class="keyword">break</span><span>; </span></span></li><li><span>        } </span></li><li class="alt"><span>        <span class="vars">$str</span><span> .= </span><span class="vars">$randNum</span><span>; </span></span></li><li><span>    } </span></li><li class="alt"><span>    <span class="keyword">return</span><span> </span><span class="vars">$str</span><span>; </span></span></li><li><span>} </span></li><li class="alt"><span><span class="vars">$color</span><span> = randomColor(); </span></span></li></ol>

3. インターネットからファイルをダウンロードします

<ol class="dp-c"><li class="alt"><span><span>set_time_limit(0); </span></span></li><li><span><span class="comment">// Supports all file types</span><span> </span></span></li><li class="alt"><span><span class="comment">// URL Here:</span><span> </span></span></li><li><span><span class="vars">$url</span><span> = </span><span class="string">'http://somsite.com/some_video.flv'</span><span>; </span></span></li><li class="alt"><span><span class="vars">$pi</span><span> = </span><span class="func">pathinfo</span><span>(</span><span class="vars">$url</span><span>); </span></span></li><li><span><span class="vars">$ext</span><span> = </span><span class="vars">$pi</span><span>[</span><span class="string">'extension'</span><span>]; </span></span></li><li class="alt"><span><span class="vars">$name</span><span> = </span><span class="vars">$pi</span><span>[</span><span class="string">'filename'</span><span>]; </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">// create a new cURL resource</span><span> </span></span></li><li><span><span class="vars">$ch</span><span> = curl_init(); </span></span></li><li class="alt"><span> </span></li><li><span><span class="comment">// set URL and other appropriate options</span><span> </span></span></li><li class="alt"><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_URL, </span><span class="vars">$url</span><span>); </span></span></li><li><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_HEADER, false); </span></span></li><li class="alt"><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_BINARYTRANSFER, true); </span></span></li><li><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_AUTOREFERER, true); </span></span></li><li class="alt"><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_FOLLOWLOCATION, true); </span></span></li><li><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_RETURNTRANSFER, true); </span></span></li><li class="alt"><span> </span></li><li><span><span class="comment">// grab URL and pass it to the browser</span><span> </span></span></li><li class="alt"><span><span class="vars">$opt</span><span> = curl_exec(</span><span class="vars">$ch</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">// close cURL resource, and free up system resources</span><span> </span></span></li><li><span>curl_close(<span class="vars">$ch</span><span>); </span></span></li><li class="alt"><span> </span></li><li><span><span class="vars">$saveFile</span><span> = </span><span class="vars">$name</span><span>.</span><span class="string">'.'</span><span>.</span><span class="vars">$ext</span><span>; </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(preg_match(</span><span class="string">"/[^0-9a-z._-]/i"</span><span>, </span><span class="vars">$saveFile</span><span>)) </span></span></li><li><span><span class="vars">$saveFile</span><span> = md5(microtime(true)).</span><span class="string">'.'</span><span>.</span><span class="vars">$ext</span><span>; </span></span></li><li class="alt"><span> </span></li><li><span><span class="vars">$handle</span><span> = </span><span class="func">fopen</span><span>(</span><span class="vars">$saveFile</span><span>, </span><span class="string">'wb'</span><span>); </span></span></li><li class="alt"><span>fwrite(<span class="vars">$handle</span><span>, </span><span class="vars">$opt</span><span>); </span></span></li><li><span>fclose(<span class="vars">$handle</span><span>); </span></span></li></ol>

4. Alexa/Google ページランク

<ol class="dp-c"><li class="alt"><span><span class="keyword">function</span><span> page_rank(</span><span class="vars">$page</span><span>, </span><span class="vars">$type</span><span> = </span><span class="string">'alexa'</span><span>){ </span></span></li><li><span><span class="keyword">switch</span><span>(</span><span class="vars">$type</span><span>){ </span></span></li><li class="alt"><span><span class="keyword">case</span><span> </span><span class="string">'alexa'</span><span>: </span></span></li><li><span><span class="vars">$url</span><span> = </span><span class="string">'http://alexa.com/siteinfo/'</span><span>; </span></span></li><li class="alt"><span><span class="vars">$handle</span><span> = </span><span class="func">fopen</span><span>(</span><span class="vars">$url</span><span>.</span><span class="vars">$page</span><span>, </span><span class="string">'r'</span><span>); </span></span></li><li><span><span class="keyword">break</span><span>; </span></span></li><li class="alt"><span><span class="keyword">case</span><span> </span><span class="string">'google'</span><span>: </span></span></li><li><span><span class="vars">$url</span><span> = </span><span class="string">'http://google.com/search?client=navclient-auto&ch=6-1484155081&features=Rank&q=info:'</span><span>; </span></span></li><li class="alt"><span><span class="vars">$handle</span><span> = </span><span class="func">fopen</span><span>(</span><span class="vars">$url</span><span>.</span><span class="string">'http://'</span><span>.</span><span class="vars">$page</span><span>, </span><span class="string">'r'</span><span>); </span></span></li><li><span><span class="keyword">break</span><span>; </span></span></li><li class="alt"><span>} </span></li><li><span><span class="vars">$content</span><span> = stream_get_contents(</span><span class="vars">$handle</span><span>); </span></span></li><li class="alt"><span>fclose(<span class="vars">$handle</span><span>); </span></span></li><li><span><span class="vars">$content</span><span> = preg_replace(</span><span class="string">"~(ntss+)~"</span><span>,</span><span class="string">''</span><span>, </span><span class="vars">$content</span><span>); </span></span></li><li class="alt"><span><span class="keyword">switch</span><span>(</span><span class="vars">$type</span><span>){ </span></span></li><li><span><span class="keyword">case</span><span> </span><span class="string">'alexa'</span><span>: </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(preg_match(</span><span class="string">'~<div class="data (downup)"><img.+?>(.+?) </div>~im'</span><span>,</span><span class="vars">$content</span><span>,</span><span class="vars">$matches</span><span>)){ </span></span></li>
<li><span><span class="keyword">return</span><span> </span><span class="vars">$matches</span><span>[2]; </span></span></li>
<li class="alt"><span>}<span class="keyword">else</span><span>{ </span></span></li>
<li><span><span class="keyword">return</span><span> FALSE; </span></span></li>
<li class="alt"><span>} </span></li>
<li><span><span class="keyword">break</span><span>; </span></span></li>
<li class="alt"><span><span class="keyword">case</span><span> </span><span class="string">'google'</span><span>: </span></span></li>
<li><span><span class="vars">$rank</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">':'</span><span>,</span><span class="vars">$content</span><span>); </span></span></li>
<li class="alt"><span><span class="keyword">if</span><span>(</span><span class="vars">$rank</span><span>[2] != </span><span class="string">''</span><span>) </span></span></li>
<li><span><span class="keyword">return</span><span> </span><span class="vars">$rank</span><span>[2]; </span></span></li>
<li class="alt"><span><span class="keyword">else</span><span> </span></span></li>
<li><span><span class="keyword">return</span><span> FALSE; </span></span></li>
<li class="alt"><span><span class="keyword">break</span><span>; </span></span></li>
<li><span><span class="keyword">default</span><span>: </span></span></li>
<li class="alt"><span><span class="keyword">return</span><span> FALSE; </span></span></li>
<li><span><span class="keyword">break</span><span>; </span></span></li>
<li class="alt"><span>} </span></li>
<li><span>} </span></li>
<li class="alt"><span><span class="comment">// Alexa Page Rank:</span><span> </span></span></li>
<li><span><span class="func">echo</span><span> </span><span class="string">'Alexa Rank: '</span><span>.page_rank(</span><span class="string">'techug.com'</span><span>); </span></span></li>
<li class="alt"><span><span class="func">echo</span><span> </span><span class="string">' '</span><span>; </span></span></li>
<li><span><span class="comment">// Google Page Rank</span><span> </span></span></li>
<li class="alt"><span><span class="func">echo</span><span> </span><span class="string">'Google Rank: '</span><span>.page_rank(</span><span class="string">'techug.com'</span><span>, </span><span class="string">'google'</span><span>); </span></span></li>
</ol>

5. ファイルの強制ダウンロード

<ol class="dp-c">
<li class="alt"><span><span class="vars">$filename</span><span> = </span><span class="vars">$_GET</span><span>[</span><span class="string">'file'</span><span>]; </span><span class="comment">//Get the fileid from the URL</span><span> </span></span></li>
<li><span><span class="comment">// Query the file ID</span><span> </span></span></li>
<li class="alt"><span><span class="vars">$query</span><span> = sprintf(</span><span class="string">"SELECT * FROM tableName WHERE id = '%s'"</span><span>,mysql_real_escape_string(</span><span class="vars">$filename</span><span>)); </span></span></li>
<li><span><span class="vars">$sql</span><span> = mysql_query(</span><span class="vars">$query</span><span>); </span></span></li>
<li class="alt"><span><span class="keyword">if</span><span>(mysql_num_rows(</span><span class="vars">$sql</span><span>) > 0){ </span></span></li>
<li><span><span class="vars">$row</span><span> = mysql_fetch_array(</span><span class="vars">$sql</span><span>); </span></span></li>
<li class="alt"><span><span class="comment">// Set some headers</span><span> </span></span></li>
<li><span>header(<span class="string">"Pragma: public"</span><span>); </span></span></li>
<li class="alt"><span>header(<span class="string">"Expires: 0"</span><span>); </span></span></li>
<li><span>header(<span class="string">"Cache-Control: must-revalidate, post-check=0, pre-check=0"</span><span>); </span></span></li>
<li class="alt"><span>header(<span class="string">"Content-Type: application/force-download"</span><span>); </span></span></li>
<li><span>header(<span class="string">"Content-Type: application/octet-stream"</span><span>); </span></span></li>
<li class="alt"><span>header(<span class="string">"Content-Type: application/download"</span><span>); </span></span></li>
<li><span>header(<span class="string">"Content-Disposition: attachment; filename="</span><span>.</span><span class="func">basename</span><span>(</span><span class="vars">$row</span><span>[</span><span class="string">'FileName'</span><span>]).</span><span class="string">";"</span><span>); </span></span></li>
<li class="alt"><span>header(<span class="string">"Content-Transfer-Encoding: binary"</span><span>); </span></span></li>
<li><span>header(<span class="string">"Content-Length: "</span><span>.</span><span class="func">filesize</span><span>(</span><span class="vars">$row</span><span>[</span><span class="string">'FileName'</span><span>])); </span></span></li>
<li class="alt"><span> </span></li>
<li><span>@readfile(<span class="vars">$row</span><span>[</span><span class="string">'FileName'</span><span>]); </span></span></li>
<li class="alt"><span><span class="func">exit</span><span>(0); </span></span></li>
<li><span>}<span class="keyword">else</span><span>{ </span></span></li>
<li class="alt"><span>header(<span class="string">"Location: /"</span><span>); </span></span></li>
<li><span><span class="func">exit</span><span>; </span></span></li>
<li class="alt"><span>} </span></li>
</ol>

6. 電子メールを使用してユーザーの Gravator アバターを表示します

<ol class="dp-j">
<li class="alt"><span><span>$gravatar_link = </span><span class="string">'http://www.gravatar.com/avatar/'</span><span> . md5($comment_author_email) . </span><span class="string">'?s=32'</span><span>; </span></span></li>
<li><span>echo <span class="string">'<img src="'</span><span> . $gravatar_link . </span><span class="string">'" />'</span><span>; </span></span></li>
</ol>

7. cURL を使用して RSS 購読数を取得します

<ol class="dp-j">
<li class="alt"><span><span>$ch = curl_init(); </span></span></li>
<li><span>curl_setopt($ch,CURLOPT_URL,<span class="string">'https://feedburner.google.com/api/awareness/1.0/GetFeedData?id=7qkrmib4r9rscbplq5qgadiiq4'</span><span>); </span></span></li>
<li class="alt"><span>curl_setopt($ch,CURLOPT_RETURNTRANSFER,<span class="number">1</span><span>); </span></span></li>
<li><span>curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,<span class="number">2</span><span>); </span></span></li>
<li class="alt"><span>$content = curl_exec($ch); </span></li>
<li><span>$subscribers = get_match(<span class="string">'/circulation="(.*)"/isU'</span><span>,$content); </span></span></li>
<li class="alt"><span>curl_close($ch); </span></li>
<li><span> </span></li>
<li class="alt"><span>八、时间差异计算 </span></li>
<li><span> </span></li>
<li class="alt"><span>function ago($time) </span></li>
<li><span>{ </span></li>
<li class="alt"><span>   $periods = array(<span class="string">"second"</span><span>, </span><span class="string">"minute"</span><span>, </span><span class="string">"hour"</span><span>, </span><span class="string">"day"</span><span>, </span><span class="string">"week"</span><span>, </span><span class="string">"month"</span><span>, </span><span class="string">"year"</span><span>, </span><span class="string">"decade"</span><span>); </span></span></li>
<li><span>$lengths = array(<span class="string">"60"</span><span>,</span><span class="string">"60"</span><span>,</span><span class="string">"24"</span><span>,</span><span class="string">"7"</span><span>,</span><span class="string">"4.35"</span><span>,</span><span class="string">"12"</span><span>,</span><span class="string">"10"</span><span>); </span></span></li>
<li class="alt"><span> </span></li>
<li><span>$now = time(); </span></li>
<li class="alt"><span> </span></li>
<li><span>$difference = $now - $time; </span></li>
<li class="alt"><span>$tense = <span class="string">"ago"</span><span>; </span></span></li>
<li><span> </span></li>
<li class="alt"><span><span class="keyword">for</span><span>($j = </span><span class="number">0</span><span>; $difference >= $lengths[$j] && $j < count($lengths)-</span><span class="number">1</span><span>; $j++) { </span></span></li><li><span>$difference /= $lengths[$j]; </span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span>$difference = round($difference); </span></li><li><span> </span></li><li class="alt"><span><span class="keyword">if</span><span>($difference != </span><span class="number">1</span><span>) { </span></span></li><li><span>$periods[$j].= <span class="string">"s"</span><span>; </span></span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span><span class="keyword">return</span><span> </span><span class="string">"$difference $periods[$j] 'ago' "</span><span>; </span></span></li><li><span>} </span></li></ol>

9. 画面キャプチャ

<ol class="dp-j"><li class="alt"><span><span>$filename= </span><span class="string">"test.jpg"</span><span>; </span></span></li><li><span>list($w, $h, $type, $attr) = getimagesize($filename); </span></li><li class="alt"><span>$src_im = imagecreatefromjpeg($filename); </span></li><li><span> </span></li><li class="alt"><span>$src_x = <span class="string">'0'</span><span>; </span><span class="comment">// begin x</span><span> </span></span></li><li><span>$src_y = <span class="string">'0'</span><span>; </span><span class="comment">// begin y</span><span> </span></span></li><li class="alt"><span>$src_w = <span class="string">'100'</span><span>; </span><span class="comment">// width</span><span> </span></span></li><li><span>$src_h = <span class="string">'100'</span><span>; </span><span class="comment">// height</span><span> </span></span></li><li class="alt"><span>$dst_x = <span class="string">'0'</span><span>; </span><span class="comment">// destination x</span><span> </span></span></li><li><span>$dst_y = <span class="string">'0'</span><span>; </span><span class="comment">// destination y</span><span> </span></span></li><li class="alt"><span> </span></li><li><span>$dst_im = imagecreatetruecolor($src_w, $src_h); </span></li><li class="alt"><span>$white = imagecolorallocate($dst_im, <span class="number">255</span><span>, </span><span class="number">255</span><span>, </span><span class="number">255</span><span>); </span></span></li><li><span>imagefill($dst_im, <span class="number">0</span><span>, </span><span class="number">0</span><span>, $white); </span></span></li><li class="alt"><span> </span></li><li><span>imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h); </span></li><li class="alt"><span> </span></li><li><span>header(<span class="string">"Content-type: image/png"</span><span>); </span></span></li><li class="alt"><span>imagepng($dst_im); </span></li><li><span>imagedestroy($dst_im); </span></li></ol>

10. Web サイトがダウンしていないか確認します

<ol class="dp-j"><li class="alt"><span><span>function Visit($url){ </span></span></li><li><span>       $agent = <span class="string">"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"</span><span>;$ch=curl_init(); </span></span></li><li class="alt"><span>curl_setopt ($ch, CURLOPT_URL,$url ); </span></li><li><span>curl_setopt($ch, CURLOPT_USERAGENT, $agent); </span></li><li class="alt"><span>curl_setopt ($ch, CURLOPT_RETURNTRANSFER, <span class="number">1</span><span>); </span></span></li><li><span>curl_setopt ($ch,CURLOPT_VERBOSE,<span class="keyword">false</span><span>); </span></span></li><li class="alt"><span>curl_setopt($ch, CURLOPT_TIMEOUT, <span class="number">5</span><span>); </span></span></li><li><span>curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE); </span></li><li class="alt"><span>curl_setopt($ch,CURLOPT_SSLVERSION,<span class="number">3</span><span>); </span></span></li><li><span>curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE); </span></li><li class="alt"><span>$page=curl_exec($ch); </span></li><li><span><span class="comment">//echo curl_error($ch);</span><span> </span></span></li><li class="alt"><span>$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); </span></li><li><span>curl_close($ch); </span></li><li class="alt"><span><span class="keyword">if</span><span>($httpcode>=</span><span class="number">200</span><span> && $httpcode<</span><span class="number">300</span><span>) </span><span class="keyword">return</span><span> </span><span class="keyword">true</span><span>; </span></span></li>
<li><span><span class="keyword">else</span><span> </span><span class="keyword">return</span><span> </span><span class="keyword">false</span><span>; </span></span></li>
<li class="alt"><span>} </span></li>
<li><span><span class="keyword">if</span><span> (Visit(</span><span class="string">"http://www.google.com"</span><span>)) </span></span></li>
<li class="alt"><span>echo <span class="string">"Website OK"</span><span>.</span><span class="string">"n"</span><span>; </span></span></li>
<li><span><span class="keyword">else</span><span> </span></span></li>
<li class="alt"><span>echo <span class="string">"Website DOWN"</span><span>; </span></span></li>
</ol>

【担当編集者:wangxueyan TEL: (010) 68476606】



声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。