Heim > php教程 > php手册 > 10 个救命的 PHP 代码片段

10 个救命的 PHP 代码片段

WBOY
Freigeben: 2016-06-21 08:53:14
Original
1278 Leute haben es durchsucht

   关键词高亮

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> highlight(</span><span class="vars">$sString</span><span>, </span><span class="vars">$aWords</span><span>) { </span></span></li>
<li><span>    <span class="keyword">if</span><span> (!</span><span class="func">is_array</span><span> (</span><span class="vars">$aWords</span><span>)  </span><span class="keyword">empty</span><span class="func">empty</span><span> (</span><span class="vars">$aWords</span><span>)  !</span><span class="func">is_string</span><span> (</span><span class="vars">$sString</span><span>)) { </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">$sWords</span><span> = implode (</span><span class="string">''</span><span>, </span><span class="vars">$aWords</span><span>); </span></span></li>
<li class="alt"><span>    <span class="keyword">return</span><span> preg_replace (</span><span class="string">'@\b('</span><span>.</span><span class="vars">$sWords</span><span>.</span><span class="string">')\b@si'</span><span>, </span><span class="string">'<strong>$1</strong>'</span><span>, </span><span class="vars">$sString</span><span>); </span></span></li>
<li><span>} </span></li>
</ol>
Nach dem Login kopieren

  获取你的Feedburner的用户

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> get_average_readers(</span><span class="vars">$feed_id</span><span>,</span><span class="vars">$interval</span><span> = 7){ </span></span></li>
<li><span>    <span class="vars">$today</span><span> = </span><span class="func">date</span><span>(</span><span class="string">'Y-m-d'</span><span>, </span><span class="func">strtotime</span><span>(</span><span class="string">"now"</span><span>)); </span></span></li>
<li class="alt"><span>    <span class="vars">$ago</span><span> = </span><span class="func">date</span><span>(</span><span class="string">'Y-m-d'</span><span>, </span><span class="func">strtotime</span><span>(</span><span class="string">"-"</span><span>.</span><span class="vars">$interval</span><span>.</span><span class="string">" days"</span><span>)); </span></span></li>
<li><span>    <span class="vars">$feed_url</span><span>=</span><span class="string">"https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri="</span><span>.</span><span class="vars">$feed_id</span><span>.</span><span class="string">"&dates="</span><span>.</span><span class="vars">$ago</span><span>.</span><span class="string">","</span><span>.</span><span class="vars">$today</span><span>; </span></span></li>
<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_RETURNTRANSFER, 1); </span></span></li>
<li class="alt"><span>    curl_setopt(<span class="vars">$ch</span><span>, CURLOPT_URL, </span><span class="vars">$feed_url</span><span>); </span></span></li>
<li><span>    <span class="vars">$data</span><span> = curl_exec(</span><span class="vars">$ch</span><span>); </span></span></li>
<li class="alt"><span>    curl_close(<span class="vars">$ch</span><span>); </span></span></li>
<li><span>    <span class="vars">$xml</span><span> = </span><span class="keyword">new</span><span> SimpleXMLElement(</span><span class="vars">$data</span><span>); </span></span></li>
<li class="alt"><span>    <span class="vars">$fb</span><span> = </span><span class="vars">$xml</span><span>->feed->entry[</span><span class="string">'circulation'</span><span>]; </span></span></li>
<li><span> </span></li>
<li class="alt"><span>    <span class="vars">$nb</span><span> = 0; </span></span></li>
<li><span>    <span class="keyword">foreach</span><span>(</span><span class="vars">$xml</span><span>->feed->children() </span><span class="keyword">as</span><span> </span><span class="vars">$circ</span><span>){ </span></span></li>
<li class="alt"><span>        <span class="vars">$nb</span><span> += </span><span class="vars">$circ</span><span>[</span><span class="string">'circulation'</span><span>]; </span></span></li>
<li><span>    } </span></li>
<li class="alt"><span> </span></li>
<li><span>    <span class="keyword">return</span><span> </span><span class="func">round</span><span>(</span><span class="vars">$nb</span><span>/</span><span class="vars">$interval</span><span>); </span></span></li>
<li class="alt"><span>} </span></li>
</ol>
Nach dem Login kopieren

  自动生成密码

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> generatePassword(</span><span class="vars">$length</span><span>=9, </span><span class="vars">$strength</span><span>=0) { </span></span></li>
<li><span>    <span class="vars">$vowels</span><span> = </span><span class="string">'aeuy'</span><span>; </span></span></li>
<li class="alt"><span>    <span class="vars">$consonants</span><span> = </span><span class="string">'bdghjmnpqrstvz'</span><span>; </span></span></li>
<li><span>    <span class="keyword">if</span><span> (</span><span class="vars">$strength</span><span> >= 1) { </span></span></li>
<li class="alt"><span>        <span class="vars">$consonants</span><span> .= </span><span class="string">'BDGHJLMNPQRSTVWXZ'</span><span>; </span></span></li>
<li><span>    } </span></li>
<li class="alt"><span>    <span class="keyword">if</span><span> (</span><span class="vars">$strength</span><span> >= 2) { </span></span></li>
<li><span>        <span class="vars">$vowels</span><span> .= </span><span class="string">"AEUY"</span><span>; </span></span></li>
<li class="alt"><span>    } </span></li>
<li><span>    <span class="keyword">if</span><span> (</span><span class="vars">$strength</span><span> >= 4) { </span></span></li>
<li class="alt"><span>        <span class="vars">$consonants</span><span> .= </span><span class="string">'23456789'</span><span>; </span></span></li>
<li><span>    } </span></li>
<li class="alt"><span>    <span class="keyword">if</span><span> (</span><span class="vars">$strength</span><span> >= 8 ) { </span></span></li>
<li><span>        <span class="vars">$vowels</span><span> .= </span><span class="string">'@#$%'</span><span>; </span></span></li>
<li class="alt"><span>    } </span></li>
<li><span> </span></li>
<li class="alt"><span>    <span class="vars">$password</span><span> = </span><span class="string">''</span><span>; </span></span></li>
<li><span>    <span class="vars">$alt</span><span> = time() % 2; </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> <span class="vars">$length</span><span>; </span><span class="vars">$i</span><span>++) { </span></span></span></li>
<li><span>        <span class="keyword">if</span><span> (</span><span class="vars">$alt</span><span> == 1) { </span></span></li>
<li class="alt"><span>            <span class="vars">$password</span><span> .= </span><span class="vars">$consonants</span><span>[(rand() % </span><span class="func">strlen</span><span>(</span><span class="vars">$consonants</span><span>))]; </span></span></li>
<li><span>            <span class="vars">$alt</span><span> = 0; </span></span></li>
<li class="alt"><span>        } <span class="keyword">else</span><span> { </span></span></li>
<li><span>            <span class="vars">$password</span><span> .= </span><span class="vars">$vowels</span><span>[(rand() % </span><span class="func">strlen</span><span>(</span><span class="vars">$vowels</span><span>))]; </span></span></li>
<li class="alt"><span>            <span class="vars">$alt</span><span> = 1; </span></span></li>
<li><span>        } </span></li>
<li class="alt"><span>    } </span></li>
<li><span>    <span class="keyword">return</span><span> </span><span class="vars">$password</span><span>; </span></span></li>
<li class="alt"><span>} </span></li>
</ol>
Nach dem Login kopieren

  压缩多个CSS文件

<ol class="dp-c">
<li class="alt"><span><span>header(</span><span class="string">'Content-type: text/css'</span><span>); </span></span></li>
<li><span>ob_start(<span class="string">"compress"</span><span>); </span></span></li>
<li class="alt"><span><span class="keyword">function</span><span> compress(</span><span class="vars">$buffer</span><span>) { </span></span></li>
<li><span>  <span class="comment">/* remove comments */</span><span> </span></span></li>
<li class="alt"><span>  <span class="vars">$buffer</span><span> = preg_replace(</span><span class="string">'!/\*[^*]*\*+([^/][^*]*\*+)*/!'</span><span>, </span><span class="string">''</span><span>, </span><span class="vars">$buffer</span><span>); </span></span></li>
<li><span>  <span class="comment">/* remove tabs, spaces, newlines, etc. */</span><span> </span></span></li>
<li class="alt"><span>  <span class="vars">$buffer</span><span> = </span><span class="func">str_replace</span><span>(</span><span class="keyword">array</span><span>(</span><span class="string">"\r\n"</span><span>, </span><span class="string">"\r"</span><span>, </span><span class="string">"\n"</span><span>, </span><span class="string">"\t"</span><span>, </span><span class="string">'  '</span><span>, </span><span class="string">'    '</span><span>, </span><span class="string">'    '</span><span>), </span><span class="string">''</span><span>, </span><span class="vars">$buffer</span><span>); </span></span></li>
<li><span>  <span class="keyword">return</span><span> </span><span class="vars">$buffer</span><span>; </span></span></li>
<li class="alt"><span>} </span></li>
<li><span> </span></li>
<li class="alt"><span><span class="comment">/* your css files */</span><span> </span></span></li>
<li><span><span class="keyword">include</span><span>(</span><span class="string">'master.css'</span><span>); </span></span></li>
<li class="alt"><span><span class="keyword">include</span><span>(</span><span class="string">'typography.css'</span><span>); </span></span></li>
<li><span><span class="keyword">include</span><span>(</span><span class="string">'grid.css'</span><span>); </span></span></li>
<li class="alt"><span><span class="keyword">include</span><span>(</span><span class="string">'print.css'</span><span>); </span></span></li>
<li><span><span class="keyword">include</span><span>(</span><span class="string">'handheld.css'</span><span>); </span></span></li>
<li class="alt"><span> </span></li>
<li><span>ob_end_flush(); </span></li>
</ol>
Nach dem Login kopieren

  获取短网址

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> getTinyUrl(</span><span class="vars">$url</span><span>) { </span></span></li>
<li><span>    <span class="keyword">return</span><span> </span><span class="func">file_get_contents</span><span>(</span><span class="string">"http://tinyurl.com/api-create.php?url="</span><span>.</span><span class="vars">$url</span><span>); </span></span></li>
<li class="alt"><span>} </span></li>
</ol>
Nach dem Login kopieren

  根据生日计算年龄

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> age(</span><span class="vars">$date</span><span>){ </span></span></li>
<li><span>    <span class="vars">$year_diff</span><span> = </span><span class="string">''</span><span>; </span></span></li>
<li class="alt"><span>    <span class="vars">$time</span><span> = </span><span class="func">strtotime</span><span>(</span><span class="vars">$date</span><span>); </span></span></li>
<li><span>    <span class="keyword">if</span><span>(FALSE === </span><span class="vars">$time</span><span>){ </span></span></li>
<li class="alt"><span>        <span class="keyword">return</span><span> </span><span class="string">''</span><span>; </span></span></li>
<li><span>    } </span></li>
<li class="alt"><span> </span></li>
<li><span>    <span class="vars">$date</span><span> = </span><span class="func">date</span><span>(</span><span class="string">'Y-m-d'</span><span>, </span><span class="vars">$time</span><span>); </span></span></li>
<li class="alt"><span>    list(<span class="vars">$year</span><span>,</span><span class="vars">$month</span><span>,</span><span class="vars">$day</span><span>) = </span><span class="func">explode</span><span>(</span><span class="string">"-"</span><span>,</span><span class="vars">$date</span><span>); </span></span></li>
<li><span>    <span class="vars">$year_diff</span><span> = </span><span class="func">date</span><span>(</span><span class="string">"Y"</span><span>) – </span><span class="vars">$year</span><span>; </span></span></li>
<li class="alt"><span>    <span class="vars">$month_diff</span><span> = </span><span class="func">date</span><span>(</span><span class="string">"m"</span><span>) – </span><span class="vars">$month</span><span>; </span></span></li>
<li><span>    <span class="vars">$day_diff</span><span> = </span><span class="func">date</span><span>(</span><span class="string">"d"</span><span>) – </span><span class="vars">$day</span><span>; </span></span></li>
<li class="alt"><span>    <span class="keyword">if</span><span> (</span><span class="vars">$day_diff</span><span> <span class="vars">$month_diff</span><span> <span class="vars">$year_diff</span><span>–; </span></span></span></span></li>
<li><span> </span></li>
<li class="alt"><span>    <span class="keyword">return</span><span> </span><span class="vars">$year_diff</span><span>; </span></span></li>
<li><span>} </span></li>
</ol>
Nach dem Login kopieren

  计算执行时间

<ol class="dp-c">
<li class="alt"><span><span class="comment">//Create a variable for start time</span><span> </span></span></li>
<li><span><span class="vars">$time_start</span><span> = microtime(true); </span></span></li>
<li class="alt"><span> </span></li>
<li><span><span class="comment">// Place your PHP/HTML/JavaScript/CSS/Etc. Here</span><span> </span></span></li>
<li class="alt"><span> </span></li>
<li><span><span class="comment">//Create a variable for end time</span><span> </span></span></li>
<li class="alt"><span><span class="vars">$time_end</span><span> = microtime(true); </span></span></li>
<li><span><span class="comment">//Subtract the two times to get seconds</span><span> </span></span></li>
<li class="alt"><span><span class="vars">$time</span><span> = </span><span class="vars">$time_end</span><span> - </span><span class="vars">$time_start</span><span>; </span></span></li>
<li><span> </span></li>
<li class="alt"><span><span class="func">echo</span><span> </span><span class="string">'Script took '</span><span>.</span><span class="vars">$time</span><span>.</span><span class="string">' seconds to execute'</span><span>; </span></span></li>
</ol>
Nach dem Login kopieren

  PHP的维护模式

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> maintenance(</span><span class="vars">$mode</span><span> = FALSE){ </span></span></li>
<li><span>    <span class="keyword">if</span><span>(</span><span class="vars">$mode</span><span>){ </span></span></li>
<li class="alt"><span>        <span class="keyword">if</span><span>(</span><span class="func">basename</span><span>(</span><span class="vars">$_SERVER</span><span>[</span><span class="string">'SCRIPT_FILENAME'</span><span>]) != </span><span class="string">'maintenance.php'</span><span>){ </span></span></li>
<li><span>            header(<span class="string">"Location: http://example.com/maintenance.php"</span><span>); </span></span></li>
<li class="alt"><span>            <span class="func">exit</span><span>; </span></span></li>
<li><span>        } </span></li>
<li class="alt"><span>    }<span class="keyword">else</span><span>{ </span></span></li>
<li><span>        <span class="keyword">if</span><span>(</span><span class="func">basename</span><span>(</span><span class="vars">$_SERVER</span><span>[</span><span class="string">'SCRIPT_FILENAME'</span><span>]) == </span><span class="string">'maintenance.php'</span><span>){ </span></span></li>
<li class="alt"><span>            header(<span class="string">"Location: http://example.com/"</span><span>); </span></span></li>
<li><span>            <span class="func">exit</span><span>; </span></span></li>
<li class="alt"><span>        } </span></li>
<li><span>    } </span></li>
<li class="alt"><span>} </span></li>
</ol>
Nach dem Login kopieren

  阻止CSS样式被缓存

<ol class="dp-c"><li class="alt"><span><span><link>
<span class="string">"/stylesheet.css?<?php  echo time(); ?>"</span><span> rel=</span><span class="string">"stylesheet"</span><span> type=</span><span class="string">"text/css"</span><span> /&glt; </span></span></span></li></ol>
Nach dem Login kopieren

  为数字增加 st\nd\rd 等

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> make_ranked(</span><span class="vars">$rank</span><span>) { </span></span></li>
<li><span>    <span class="vars">$last</span><span> = </span><span class="func">substr</span><span>( </span><span class="vars">$rank</span><span>, -1 ); </span></span></li>
<li class="alt"><span>    <span class="vars">$seclast</span><span> = </span><span class="func">substr</span><span>( </span><span class="vars">$rank</span><span>, -2, -1 ); </span></span></li>
<li><span>    <span class="keyword">if</span><span>( </span><span class="vars">$last</span><span> > 3  </span><span class="vars">$last</span><span> == 0 ) </span><span class="vars">$ext</span><span> = </span><span class="string">'th'</span><span>; </span></span></li>
<li class="alt"><span>    <span class="keyword">else</span><span> </span><span class="keyword">if</span><span>( </span><span class="vars">$last</span><span> == 3 ) </span><span class="vars">$ext</span><span> = </span><span class="string">'rd'</span><span>; </span></span></li>
<li><span>    <span class="keyword">else</span><span> </span><span class="keyword">if</span><span>( </span><span class="vars">$last</span><span> == 2 ) </span><span class="vars">$ext</span><span> = </span><span class="string">'nd'</span><span>; </span></span></li>
<li class="alt"><span>    <span class="keyword">else</span><span> </span><span class="vars">$ext</span><span> = </span><span class="string">'st'</span><span>;  </span></span></li>
<li><span> </span></li>
<li class="alt"><span>    <span class="keyword">if</span><span>( </span><span class="vars">$last</span><span> == 1 && </span><span class="vars">$seclast</span><span> == 1) </span><span class="vars">$ext</span><span> = </span><span class="string">'th'</span><span>; </span></span></li>
<li><span>    <span class="keyword">if</span><span>( </span><span class="vars">$last</span><span> == 2 && </span><span class="vars">$seclast</span><span> == 1) </span><span class="vars">$ext</span><span> = </span><span class="string">'th'</span><span>; </span></span></li>
<li class="alt"><span>    <span class="keyword">if</span><span>( </span><span class="vars">$last</span><span> == 3 && </span><span class="vars">$seclast</span><span> == 1) </span><span class="vars">$ext</span><span> = </span><span class="string">'th'</span><span>;  </span></span></li>
<li><span> </span></li>
<li class="alt"><span>    <span class="keyword">return</span><span> </span><span class="vars">$rank</span><span>.</span><span class="vars">$ext</span><span>; </span></span></li>
<li><span>} </span></li>
</ol>
Nach dem Login kopieren




Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage