10个典型实用的PHP代码片段
本文将介绍10个经常会用到的PHP代码片段,包括黑名单过滤、随机颜色生成器、从网上下载文件、Alexa/Google Page Rank、强制下载文件、用Email显示用户的Gravator头像、用cURL获取RSS订阅数、截取图片、检查网站是否宕机。
一、黑名单过滤
<ol class="dp-c"><li class="alt"><span><span class="keyword">function</span><span> is_spam(</span><span class="vars">$text</span><span>, </span><span class="vars">$file</span><span>, </span><span class="vars">$split</span><span> = </span><span class="string">':'</span><span>, </span><span class="vars">$regex</span><span> = false){ </span></span></li><li><span> <span class="vars">$handle</span><span> = </span><span class="func">fopen</span><span>(</span><span class="vars">$file</span><span>, </span><span class="string">'rb'</span><span>); </span></span></li><li class="alt"><span> <span class="vars">$contents</span><span> = </span><span class="func">fread</span><span>(</span><span class="vars">$handle</span><span>, </span><span class="func">filesize</span><span>(</span><span class="vars">$file</span><span>)); </span></span></li><li><span> fclose(<span class="vars">$handle</span><span>); </span></span></li><li class="alt"><span> <span class="vars">$lines</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">"n"</span><span>, </span><span class="vars">$contents</span><span>); </span></span></li><li><span><span class="vars">$arr</span><span> = </span><span class="keyword">array</span><span>(); </span></span></li><li class="alt"><span><span class="keyword">foreach</span><span>(</span><span class="vars">$lines</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$line</span><span>){ </span></span></li><li><span>list(<span class="vars">$word</span><span>, </span><span class="vars">$count</span><span>) = </span><span class="func">explode</span><span>(</span><span class="vars">$split</span><span>, </span><span class="vars">$line</span><span>); </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(</span><span class="vars">$regex</span><span>) </span></span></li><li><span><span class="vars">$arr</span><span>[</span><span class="vars">$word</span><span>] = </span><span class="vars">$count</span><span>; </span></span></li><li class="alt"><span><span class="keyword">else</span><span> </span></span></li><li><span><span class="vars">$arr</span><span>[preg_quote(</span><span class="vars">$word</span><span>)] = </span><span class="vars">$count</span><span>; </span></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>, </span><span class="func">array_keys</span><span>(</span><span class="vars">$arr</span><span>)).</span><span class="string">"~"</span><span>, </span><span class="vars">$text</span><span>, </span><span class="vars">$matches</span><span>); </span></span></li><li class="alt"><span><span class="vars">$temp</span><span> = </span><span class="keyword">array</span><span>(); </span></span></li><li><span><span class="keyword">foreach</span><span>(</span><span class="vars">$matches</span><span>[0] </span><span class="keyword">as</span><span> </span><span class="vars">$match</span><span>){ </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(!in_array(</span><span class="vars">$match</span><span>, </span><span class="vars">$temp</span><span>)){ </span></span></li><li><span><span class="vars">$temp</span><span>[</span><span class="vars">$match</span><span>] = </span><span class="vars">$temp</span><span>[</span><span class="vars">$match</span><span>] + 1; </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(</span><span class="vars">$temp</span><span>[</span><span class="vars">$match</span><span>] >= </span><span class="vars">$arr</span><span>[</span><span class="vars">$word</span><span>]) </span></span></li><li><span><span class="keyword">return</span><span> true; </span></span></li><li class="alt"><span>} </span></li><li><span>} </span></li><li class="alt"><span><span class="keyword">return</span><span> false; </span></span></li><li><span>} </span></li><li class="alt"><span> </span></li><li><span><span class="vars">$file</span><span> = </span><span class="string">'spam.txt'</span><span>; </span></span></li><li class="alt"><span><span class="vars">$str</span><span> = </span><span class="string">'This string has cat, dog word'</span><span>; </span></span></li><li><span><span class="keyword">if</span><span>(is_spam(</span><span class="vars">$str</span><span>, </span><span class="vars">$file</span><span>)) </span></span></li><li class="alt"><span><span class="func">echo</span><span> </span><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><span class="func">echo</span><span> </span><span class="string">'this is not spam'</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span>ab:3 </span></li><li><span>dog:3 </span></li><li class="alt"><span>cat:2 </span></li><li><span>monkey:2 </span></li></ol>
二、随机颜色生成器
<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>
三、从网上下载文件
<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>
四、Alexa/Google Page Rank
<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">"~(n|t|ss+)~"</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 (down|up)"><img .+? alt="10个典型实用的PHP代码片段" >(.+?) </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>
五、强制下载文件
<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>
六、用Email显示用户的Gravator头像
<ol class="dp-c"><li class="alt"><span><span class="vars">$gravatar_link</span><span> = </span><span class="string">'http://www.gravatar.com/avatar/'</span><span> . md5(</span><span class="vars">$comment_author_email</span><span>) . </span><span class="string">'?s=32'</span><span>; </span></span></li><li><span><span class="func">echo</span><span> </span><span class="string">'<img src="'</span alt="10个典型实用的PHP代码片段" ><span> . </span><span class="vars">$gravatar_link</span><span> . </span><span class="string">'" />'</span><span>; </span></span></li></ol>
七、用cURL获取RSS订阅数
<ol class="dp-c"><li class="alt"><span><span class="vars">$ch</span><span> = curl_init(); </span></span></li><li><span>curl_setopt(<span class="vars">$ch</span><span>,CURLOPT_URL,</span><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(<span class="vars">$ch</span><span>,CURLOPT_RETURNTRANSFER,1); </span></span></li><li><span>curl_setopt(<span class="vars">$ch</span><span>,CURLOPT_CONNECTTIMEOUT,2); </span></span></li><li class="alt"><span><span class="vars">$content</span><span> = curl_exec(</span><span class="vars">$ch</span><span>); </span></span></li><li><span><span class="vars">$subscribers</span><span> = get_match(</span><span class="string">'/circulation="(.*)"/isU'</span><span>,</span><span class="vars">$content</span><span>); </span></span></li><li class="alt"><span>curl_close(<span class="vars">$ch</span><span>); </span></span></li></ol>
八、时间差异计算
<ol class="dp-j"><li class="alt"><span><span>function ago($time) </span></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>
九、截取图片
<ol class="dp-c"><li class="alt"><span><span class="vars">$filename</span><span>= </span><span class="string">"test.jpg"</span><span>; </span></span></li><li><span>list(<span class="vars">$w</span><span>, </span><span class="vars">$h</span><span>, </span><span class="vars">$type</span><span>, </span><span class="vars">$attr</span><span>) = </span><span class="func">getimagesize</span><span>(</span><span class="vars">$filename</span><span>); </span></span></li><li class="alt"><span><span class="vars">$src_im</span><span> = imagecreatefromjpeg(</span><span class="vars">$filename</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$src_x</span><span> = </span><span class="string">'0'</span><span>; </span><span class="comment">// begin x</span><span> </span></span></li><li><span><span class="vars">$src_y</span><span> = </span><span class="string">'0'</span><span>; </span><span class="comment">// begin y</span><span> </span></span></li><li class="alt"><span><span class="vars">$src_w</span><span> = </span><span class="string">'100'</span><span>; </span><span class="comment">// width</span><span> </span></span></li><li><span><span class="vars">$src_h</span><span> = </span><span class="string">'100'</span><span>; </span><span class="comment">// height</span><span> </span></span></li><li class="alt"><span><span class="vars">$dst_x</span><span> = </span><span class="string">'0'</span><span>; </span><span class="comment">// destination x</span><span> </span></span></li><li><span><span class="vars">$dst_y</span><span> = </span><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><span class="vars">$dst_im</span><span> = imagecreatetruecolor(</span><span class="vars">$src_w</span><span>, </span><span class="vars">$src_h</span><span>); </span></span></li><li class="alt"><span><span class="vars">$white</span><span> = imagecolorallocate(</span><span class="vars">$dst_im</span><span>, 255, 255, 255); </span></span></li><li><span>imagefill(<span class="vars">$dst_im</span><span>, 0, 0, </span><span class="vars">$white</span><span>); </span></span></li><li class="alt"><span> </span></li><li><span>imagecopy(<span class="vars">$dst_im</span><span>, </span><span class="vars">$src_im</span><span>, </span><span class="vars">$dst_x</span><span>, </span><span class="vars">$dst_y</span><span>, </span><span class="vars">$src_x</span><span>, </span><span class="vars">$src_y</span><span>, </span><span class="vars">$src_w</span><span>, </span><span class="vars">$src_h</span><span>); </span></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(<span class="vars">$dst_im</span><span>); </span></span></li><li><span>imagedestroy(<span class="vars">$dst_im</span><span>); </span></span></li></ol>
十、检查网站是否宕机
<ol class="dp-c"><li class="alt"><span><span class="keyword">function</span><span> Visit(</span><span class="vars">$url</span><span>){ </span></span></li><li><span> <span class="vars">$agent</span><span> = </span><span class="string">"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"</span><span>;</span><span class="vars">$ch</span><span>=curl_init(); </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_USERAGENT, </span><span class="vars">$agent</span><span>); </span></span></li><li class="alt"><span>curl_setopt (<span class="vars">$ch</span><span>, CURLOPT_RETURNTRANSFER, 1); </span></span></li><li><span>curl_setopt (<span class="vars">$ch</span><span>,CURLOPT_VERBOSE,false); </span></span></li><li class="alt"><span>curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_TIMEOUT, 5); </span></span></li><li><span>curl_setopt(<span class="vars">$ch</span><span>,CURLOPT_SSL_VERIFYPEER, FALSE); </span></span></li><li class="alt"><span>curl_setopt(<span class="vars">$ch</span><span>,CURLOPT_SSLVERSION,3); </span></span></li><li><span>curl_setopt(<span class="vars">$ch</span><span>,CURLOPT_SSL_VERIFYHOST, FALSE); </span></span></li><li class="alt"><span><span class="vars">$page</span><span>=curl_exec(</span><span class="vars">$ch</span><span>); </span></span></li><li><span><span class="comment">//echo curl_error($ch);</span><span> </span></span></li><li class="alt"><span><span class="vars">$httpcode</span><span> = curl_getinfo(</span><span class="vars">$ch</span><span>, CURLINFO_HTTP_CODE); </span></span></li><li><span>curl_close(<span class="vars">$ch</span><span>); </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(</span><span class="vars">$httpcode</span><span>>=200 && </span><span class="vars">$httpcode</span><span><300) </span><span class="keyword">return</span><span> true; </span></span></li><li><span><span class="keyword">else</span><span> </span><span class="keyword">return</span><span> false; </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><span class="func">echo</span><span> </span><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><span class="func">echo</span><span> </span><span class="string">"Website DOWN"</span><span>; </span></span></li></ol>

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
