首页 > php教程 > php手册 > 正文

再来二十一段救命的PHP代码

WBOY
发布: 2016-06-21 08:53:00
原创
850 人浏览过

   1. PHP可阅读随机字符串

  此代码将创建一个可阅读的字符串,使其更接近词典中的单词,实用且具有密码验证功能。

<ol class="dp-c">
<li class="alt"><span><span class="comment">/**************</span> </span></li>
<li><span><span class="comment">*@length - length of random string (must be a multiple of 2)</span> </span></li>
<li class="alt"><span><span class="comment">**************/</span><span> </span></span></li>
<li><span><span class="keyword">function</span><span> readable_random_string(</span><span class="vars">$length</span><span> = 6){ </span></span></li>
<li class="alt"><span>    <span class="vars">$conso</span><span>=</span><span class="keyword">array</span><span>(</span><span class="string">"b"</span><span>,</span><span class="string">"c"</span><span>,</span><span class="string">"d"</span><span>,</span><span class="string">"f"</span><span>,</span><span class="string">"g"</span><span>,</span><span class="string">"h"</span><span>,</span><span class="string">"j"</span><span>,</span><span class="string">"k"</span><span>,</span><span class="string">"l"</span><span>, </span></span></li>
<li><span>    <span class="string">"m"</span><span>,</span><span class="string">"n"</span><span>,</span><span class="string">"p"</span><span>,</span><span class="string">"r"</span><span>,</span><span class="string">"s"</span><span>,</span><span class="string">"t"</span><span>,</span><span class="string">"v"</span><span>,</span><span class="string">"w"</span><span>,</span><span class="string">"x"</span><span>,</span><span class="string">"y"</span><span>,</span><span class="string">"z"</span><span>); </span></span></li>
<li class="alt"><span>    <span class="vars">$vocal</span><span>=</span><span class="keyword">array</span><span>(</span><span class="string">"a"</span><span>,</span><span class="string">"e"</span><span>,</span><span class="string">"i"</span><span>,</span><span class="string">"o"</span><span>,</span><span class="string">"u"</span><span>); </span></span></li>
<li><span>    <span class="vars">$password</span><span>=</span><span class="string">""</span><span>; </span></span></li>
<li class="alt"><span>    srand ((double)microtime()*1000000); </span></li>
<li><span>    <span class="vars">$max</span><span> = </span><span class="vars">$length</span><span>/2; </span></span></li>
<li class="alt"><span>    <span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=1; </span><span class="vars">$i</span><span><span class="vars">$max</span><span>; </span><span class="vars">$i</span><span>++) </span></span></span></li>
<li><span>    { </span></li>
<li class="alt"><span>    <span class="vars">$password</span><span>.=</span><span class="vars">$conso</span><span>[rand(0,19)]; </span></span></li>
<li><span>    <span class="vars">$password</span><span>.=</span><span class="vars">$vocal</span><span>[rand(0,4)]; </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>
登录后复制

  2. PHP生成一个随机字符串

  如果不需要可阅读的字符串,使用此函数替代,即可创建一个随机字符串,作为用户的随机密码等。

<ol class="dp-c">
<li class="alt"><span><span class="comment">/*************</span> </span></li>
<li><span><span class="comment">*@l - length of random string</span> </span></li>
<li class="alt"><span><span class="comment">*/</span><span> </span></span></li>
<li><span><span class="keyword">function</span><span> generate_rand(</span><span class="vars">$l</span><span>){ </span></span></li>
<li class="alt"><span>  <span class="vars">$c</span><span>= </span><span class="string">"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"</span><span>; </span></span></li>
<li><span>  srand((double)microtime()*1000000); </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">$l</span><span>; </span><span class="vars">$i</span><span>++) { </span></span></span></li>
<li><span>      <span class="vars">$rand</span><span>.= </span><span class="vars">$c</span><span>[rand()%</span><span class="func">strlen</span><span>(</span><span class="vars">$c</span><span>)]; </span></span></li>
<li class="alt"><span>  } </span></li>
<li><span>  <span class="keyword">return</span><span> </span><span class="vars">$rand</span><span>; </span></span></li>
<li class="alt"><span>} </span></li>
</ol>
登录后复制

  3. PHP编码电子邮件地址

  使用此代码,可以将任何电子邮件地址编码为 html 字符实体,以防止被垃圾邮件程序收集。

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> encode_email(</span><span class="vars">$email</span><span>=</span><span class="string">'info@domain.com'</span><span>, </span><span class="vars">$linkText</span><span>=</span><span class="string">'Contact Us'</span><span>, </span><span class="vars">$attrs</span><span> =</span><span class="string">'class="emailencoder"'</span><span> ) </span></span></li>
<li><span>{ </span></li>
<li class="alt"><span>    <span class="comment">// remplazar aroba y puntos</span><span> </span></span></li>
<li><span>    <span class="vars">$email</span><span> = </span><span class="func">str_replace</span><span>(</span><span class="string">'@'</span><span>, </span><span class="string">'@'</span><span>, </span><span class="vars">$email</span><span>); </span></span></li>
<li class="alt"><span>    <span class="vars">$email</span><span> = </span><span class="func">str_replace</span><span>(</span><span class="string">'.'</span><span>, </span><span class="string">'.'</span><span>, </span><span class="vars">$email</span><span>); </span></span></li>
<li><span>    <span class="vars">$email</span><span> = </span><span class="func">str_split</span><span>(</span><span class="vars">$email</span><span>, 5);   </span></span></li>
<li class="alt"><span> </span></li>
<li><span>    <span class="vars">$linkText</span><span> = </span><span class="func">str_replace</span><span>(</span><span class="string">'@'</span><span>, </span><span class="string">'@'</span><span>, </span><span class="vars">$linkText</span><span>); </span></span></li>
<li class="alt"><span>    <span class="vars">$linkText</span><span> = </span><span class="func">str_replace</span><span>(</span><span class="string">'.'</span><span>, </span><span class="string">'.'</span><span>, </span><span class="vars">$linkText</span><span>); </span></span></li>
<li><span>    <span class="vars">$linkText</span><span> = </span><span class="func">str_split</span><span>(</span><span class="vars">$linkText</span><span>, 5);   </span></span></li>
<li class="alt"><span> </span></li>
<li><span>    <span class="vars">$part1</span><span> = </span><span class="string">'<a><span>; </span></a></span></span></li>
<li class="alt"><span>    <span class="vars">$part2</span><span> = </span><span class="string">'ilto:'</span><span>; </span></span></li>
<li><span>    <span class="vars">$part3</span><span> = </span><span class="string">'" '</span><span>. </span><span class="vars">$attrs</span><span> .</span><span class="string">' >'</span><span>; </span></span></li>
<li class="alt"><span>    <span class="vars">$part4</span><span> = </span><span class="string">''</span><span>;   </span></span></li>
<li><span> </span></li>
<li class="alt"><span>    <span class="vars">$encoded</span><span> = </span><span class="string">'<script>'</script></span><span>; </span></span></li>
<li><span>    <span class="vars">$encoded</span><span> .= </span><span class="string">"document.write('$part1');"</span><span>; </span></span></li>
<li class="alt"><span>    <span class="vars">$encoded</span><span> .= </span><span class="string">"document.write('$part2');"</span><span>; </span></span></li>
<li><span>    <span class="keyword">foreach</span><span>(</span><span class="vars">$email</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$e</span><span>) </span></span></li>
<li class="alt"><span>    { </span></li>
<li><span>            <span class="vars">$encoded</span><span> .= </span><span class="string">"document.write('$e');"</span><span>; </span></span></li>
<li class="alt"><span>    } </span></li>
<li><span>    <span class="vars">$encoded</span><span> .= </span><span class="string">"document.write('$part3');"</span><span>; </span></span></li>
<li class="alt"><span>    <span class="keyword">foreach</span><span>(</span><span class="vars">$linkText</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$l</span><span>) </span></span></li>
<li><span>    { </span></li>
<li class="alt"><span>            <span class="vars">$encoded</span><span> .= </span><span class="string">"document.write('$l');"</span><span>; </span></span></li>
<li><span>    } </span></li>
<li class="alt"><span>    <span class="vars">$encoded</span><span> .= </span><span class="string">"document.write('$part4');"</span><span>; </span></span></li>
<li><span>    <span class="vars">$encoded</span><span> .= </span><span class="string">''</span><span>;   </span></span></li>
<li class="alt"><span> </span></li>
<li><span>    <span class="keyword">return</span><span> </span><span class="vars">$encoded</span><span>; </span></span></li>
<li class="alt"><span>} </span></li>
</ol>
登录后复制

  4. PHP验证邮件地址

  电子邮件验证也许是中最常用的网页表单验证,此代码除了验证电子邮件地址,也可以选择检查邮件域所属 DNS 中的 MX 记录,使邮件验证功能更加强大。

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> is_valid_email(</span><span class="vars">$email</span><span>, </span><span class="vars">$test_mx</span><span> = false) </span></span></li>
<li><span>{ </span></li>
<li class="alt"><span>    <span class="keyword">if</span><span>(</span><span class="func">eregi</span><span>(</span><span class="string">"^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$"</span><span>, </span><span class="vars">$email</span><span>)) </span></span></li>
<li><span>        <span class="keyword">if</span><span>(</span><span class="vars">$test_mx</span><span>) </span></span></li>
<li class="alt"><span>        { </span></li>
<li><span>            list(<span class="vars">$username</span><span>, </span><span class="vars">$domain</span><span>) = split(</span><span class="string">"@"</span><span>, </span><span class="vars">$email</span><span>); </span></span></li>
<li class="alt"><span>            <span class="keyword">return</span><span> </span><span class="func">getmxrr</span><span>(</span><span class="vars">$domain</span><span>, </span><span class="vars">$mxrecords</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">return</span><span> true; </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>
</ol>
登录后复制

  5. PHP列出目录内容

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> list_files(</span><span class="vars">$dir</span><span>) </span></span></li>
<li><span>{ </span></li>
<li class="alt"><span>    <span class="keyword">if</span><span>(</span><span class="func">is_dir</span><span>(</span><span class="vars">$dir</span><span>)) </span></span></li>
<li><span>    { </span></li>
<li class="alt"><span>        <span class="keyword">if</span><span>(</span><span class="vars">$handle</span><span> = opendir(</span><span class="vars">$dir</span><span>)) </span></span></li>
<li><span>        { </span></li>
<li class="alt"><span>            <span class="keyword">while</span><span>((</span><span class="vars">$file</span><span> = readdir(</span><span class="vars">$handle</span><span>)) !== false) </span></span></li>
<li><span>            { </span></li>
<li class="alt"><span>                <span class="keyword">if</span><span>(</span><span class="vars">$file</span><span> != </span><span class="string">"."</span><span> && </span><span class="vars">$file</span><span> != </span><span class="string">".."</span><span> && </span><span class="vars">$file</span><span> != </span><span class="string">"Thumbs.db"</span><span>) </span></span></li>
<li><span>                { </span></li>
<li class="alt"><span>                    <span class="func">echo</span><span> </span><span class="string">'<a><span>.</span><span class="vars">$dir</span><span>.</span><span class="vars">$file</span><span>.</span><span class="string">'">'</span><span>.</span><span class="vars">$file</span><span>.</span><span class="string">'</span></a><br>'</span><span>.</span><span class="string">"\n"</span><span>; </span></span></li>
<li><span>                } </span></li>
<li class="alt"><span>            } </span></li>
<li><span>            <span class="func">closedir</span><span>(</span><span class="vars">$handle</span><span>); </span></span></li>
<li class="alt"><span>        } </span></li>
<li><span>    } </span></li>
<li class="alt"><span>} </span></li>
</ol>
登录后复制

  6. PHP销毁目录

  删除一个目录,包括它的内容。

<ol class="dp-c">
<li class="alt"><span><span class="comment">/*****</span> </span></li>
<li><span><span class="comment">*@dir - Directory to destroy</span> </span></li>
<li class="alt"><span><span class="comment">*@virtual[optional]- whether a virtual directory</span> </span></li>
<li><span><span class="comment">*/</span><span> </span></span></li>
<li class="alt"><span><span class="keyword">function</span><span> destroyDir(</span><span class="vars">$dir</span><span>, </span><span class="vars">$virtual</span><span> = false) </span></span></li>
<li><span>{ </span></li>
<li class="alt"><span>    <span class="vars">$ds</span><span> = DIRECTORY_SEPARATOR; </span></span></li>
<li><span>    <span class="vars">$dir</span><span> = </span><span class="vars">$virtual</span><span> ? </span><span class="func">realpath</span><span>(</span><span class="vars">$dir</span><span>) : </span><span class="vars">$dir</span><span>; </span></span></li>
<li class="alt"><span>    <span class="vars">$dir</span><span> = </span><span class="func">substr</span><span>(</span><span class="vars">$dir</span><span>, -1) == </span><span class="vars">$ds</span><span> ? </span><span class="func">substr</span><span>(</span><span class="vars">$dir</span><span>, 0, -1) : </span><span class="vars">$dir</span><span>; </span></span></li>
<li><span>    <span class="keyword">if</span><span> (</span><span class="func">is_dir</span><span>(</span><span class="vars">$dir</span><span>) && </span><span class="vars">$handle</span><span> = opendir(</span><span class="vars">$dir</span><span>)) </span></span></li>
<li class="alt"><span>    { </span></li>
<li><span>        <span class="keyword">while</span><span> (</span><span class="vars">$file</span><span> = readdir(</span><span class="vars">$handle</span><span>)) </span></span></li>
<li class="alt"><span>        { </span></li>
<li><span>            <span class="keyword">if</span><span> (</span><span class="vars">$file</span><span> == </span><span class="string">'.'</span><span>  </span><span class="vars">$file</span><span> == </span><span class="string">'..'</span><span>) </span></span></li>
<li class="alt"><span>            { </span></li>
<li><span>                <span class="keyword">continue</span><span>; </span></span></li>
<li class="alt"><span>            } </span></li>
<li><span>            <span class="keyword">elseif</span><span> (</span><span class="func">is_dir</span><span>(</span><span class="vars">$dir</span><span>.</span><span class="vars">$ds</span><span>.</span><span class="vars">$file</span><span>)) </span></span></li>
<li class="alt"><span>            { </span></li>
<li><span>                destroyDir(<span class="vars">$dir</span><span>.</span><span class="vars">$ds</span><span>.</span><span class="vars">$file</span><span>); </span></span></li>
<li class="alt"><span>            } </span></li>
<li><span>            <span class="keyword">else</span><span> </span></span></li>
<li class="alt"><span>            { </span></li>
<li><span>                unlink(<span class="vars">$dir</span><span>.</span><span class="vars">$ds</span><span>.</span><span class="vars">$file</span><span>); </span></span></li>
<li class="alt"><span>            } </span></li>
<li><span>        } </span></li>
<li class="alt"><span>        <span class="func">closedir</span><span>(</span><span class="vars">$handle</span><span>); </span></span></li>
<li><span>        <span class="func">rmdir</span><span>(</span><span class="vars">$dir</span><span>); </span></span></li>
<li class="alt"><span>        <span class="keyword">return</span><span> true; </span></span></li>
<li><span>    } </span></li>
<li class="alt"><span>    <span class="keyword">else</span><span> </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>
</ol>
登录后复制

  7. PHP解析 JSON 数据

  与大多数流行的 Web 服务如 twitter 通过开放 API 来提供数据一样,它总是能够知道如何解析 API 数据的各种传送格式,包括 JSON,XML 等等。

<ol class="dp-c">
<li class="alt"><span><span class="vars">$json_string</span><span>=</span><span class="string">'{"id":1,"name":"foo","email":"foo@foobar.com","interest":["wordpress","php"]} '</span><span>; </span></span></li>
<li><span><span class="vars">$obj</span><span>=json_decode(</span><span class="vars">$json_string</span><span>); </span></span></li>
<li class="alt"><span><span class="func">echo</span><span> </span><span class="vars">$obj</span><span>->name; </span><span class="comment">//prints foo</span><span> </span></span></li>
<li><span><span class="func">echo</span><span> </span><span class="vars">$obj</span><span>->interest[1]; </span><span class="comment">//prints php</span><span> </span></span></li>
</ol>
登录后复制

  8. PHP解析 XML 数据

<ol class="dp-c">
<li class="alt"><span><span class="comment">//xml string</span><span> </span></span></li>
<li><span><span class="vars">$xml_string</span><span>="<?xml  version=</span><span class="string">'1.0'</span><span>?> </span></span></span></li>
<li class="alt"><span><users> </users></span></li>
<li>
<span><user class="string">'398'</user></span><span>> </span>
</li>
<li class="alt"><span><name>Foo</name> </span></li>
<li><span><email>foo@bar.com </email></span></li>
<li class="alt"><span> </span></li>
<li>
<span><user class="string">'867'</user></span><span>> </span>
</li>
<li class="alt"><span><name>Foobar</name> </span></li>
<li><span><email>foobar@foo.com </email></span></li>
<li class="alt"><span> </span></li>
<li><span>";  </span></li>
<li class="alt"><span> </span></li>
<li><span><span class="comment">//load the xml string using simplexml</span><span> </span></span></li>
<li class="alt"><span><span class="vars">$xml</span><span> = simplexml_load_string(</span><span class="vars">$xml_string</span><span>);  </span></span></li>
<li><span> </span></li>
<li class="alt"><span><span class="comment">//loop through the each node of user</span><span> </span></span></li>
<li><span><span class="keyword">foreach</span><span> (</span><span class="vars">$xml</span><span>->user </span><span class="keyword">as</span><span> </span><span class="vars">$user</span><span>) </span></span></li>
<li class="alt"><span>{ </span></li>
<li><span><span class="comment">//access attribute</span><span> </span></span></li>
<li class="alt"><span><span class="func">echo</span><span> </span><span class="vars">$user</span><span>[</span><span class="string">'id'</span><span>], </span><span class="string">' '</span><span>; </span></span></li>
<li><span><span class="comment">//subnodes are accessed by -> operator</span><span> </span></span></li>
<li class="alt"><span><span class="func">echo</span><span> </span><span class="vars">$user</span><span>->name, </span><span class="string">' '</span><span>; </span></span></li>
<li><span><span class="func">echo</span><span> </span><span class="vars">$user</span><span>->email, </span><span class="string">'<br>'</span><span>; </span></span></li>
<li class="alt"><span>} </span></li>
</ol>
登录后复制

  9. PHP创建日志缩略名

  创建用户友好的日志缩略名。

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> create_slug(</span><span class="vars">$string</span><span>){ </span></span></li>
<li><span><span class="vars">$slug</span><span>=preg_replace(</span><span class="string">'/[^A-Za-z0-9-]+/'</span><span>, </span><span class="string">'-'</span><span>, </span><span class="vars">$string</span><span>); </span></span></li>
<li class="alt"><span><span class="keyword">return</span><span> </span><span class="vars">$slug</span><span>; </span></span></li>
<li><span>} </span></li>
</ol>
登录后复制

  10. PHP获取客户端真实 IP 地址

  该函数将获取用户的真实 IP 地址,即便他使用代理服务器。

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> getRealIpAddr() </span></span></li>
<li><span>{ </span></li>
<li class="alt"><span>    <span class="keyword">if</span><span> (!emptyempty(</span><span class="vars">$_SERVER</span><span>[</span><span class="string">'HTTP_CLIENT_IP'</span><span>])) </span></span></li>
<li><span>    { </span></li>
<li class="alt"><span>        <span class="vars">$ip</span><span>=</span><span class="vars">$_SERVER</span><span>[</span><span class="string">'HTTP_CLIENT_IP'</span><span>]; </span></span></li>
<li><span>    } </span></li>
<li class="alt"><span>    <span class="keyword">elseif</span><span> (!emptyempty(</span><span class="vars">$_SERVER</span><span>[</span><span class="string">'HTTP_X_FORWARDED_FOR'</span><span>])) </span></span></li>
<li><span>    <span class="comment">//to check ip is pass from proxy</span><span> </span></span></li>
<li class="alt"><span>    { </span></li>
<li><span>        <span class="vars">$ip</span><span>=</span><span class="vars">$_SERVER</span><span>[</span><span class="string">'HTTP_X_FORWARDED_FOR'</span><span>]; </span></span></li>
<li class="alt"><span>    } </span></li>
<li><span>    <span class="keyword">else</span><span> </span></span></li>
<li class="alt"><span>    { </span></li>
<li><span>        <span class="vars">$ip</span><span>=</span><span class="vars">$_SERVER</span><span>[</span><span class="string">'REMOTE_ADDR'</span><span>]; </span></span></li>
<li class="alt"><span>    } </span></li>
<li><span>    <span class="keyword">return</span><span> </span><span class="vars">$ip</span><span>; </span></span></li>
<li class="alt"><span>} </span></li>
</ol>
登录后复制

  11. PHP强制性文件下载

  为用户提供强制性的文件下载功能。

<ol class="dp-c">
<li class="alt"><span><span class="comment">/********************</span> </span></li>
<li><span><span class="comment">*@file - path to file</span> </span></li>
<li class="alt"><span><span class="comment">*/</span><span> </span></span></li>
<li><span><span class="keyword">function</span><span> force_download(</span><span class="vars">$file</span><span>) </span></span></li>
<li class="alt"><span>{ </span></li>
<li><span><span class="keyword">if</span><span> ((isset(</span><span class="vars">$file</span><span>))&&(</span><span class="func">file_exists</span><span>(</span><span class="vars">$file</span><span>))) { </span></span></li>
<li class="alt"><span>header(<span class="string">"Content-length: "</span><span>.</span><span class="func">filesize</span><span>(</span><span class="vars">$file</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-Disposition: attachment; filename="'</span><span> . </span><span class="vars">$file</span><span> . </span><span class="string">'"'</span><span>); </span></span></li>
<li><span>readfile(<span class="string">"$file"</span><span>); </span></span></li>
<li class="alt"><span>} <span class="keyword">else</span><span> { </span></span></li>
<li><span><span class="func">echo</span><span> </span><span class="string">"No file selected"</span><span>; </span></span></li>
<li class="alt"><span>} </span></li>
<li><span>} </span></li>
</ol>
登录后复制

  12. PHP创建标签云

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> getCloud( </span><span class="vars">$data</span><span> = </span><span class="keyword">array</span><span>(), </span><span class="vars">$minFontSize</span><span> = 12, </span><span class="vars">$maxFontSize</span><span> = 30 ) </span></span></li>
<li><span>{ </span></li>
<li class="alt"><span><span class="vars">$minimumCount</span><span> = min( </span><span class="func">array_values</span><span>( </span><span class="vars">$data</span><span> ) ); </span></span></li>
<li><span><span class="vars">$maximumCount</span><span> = max( </span><span class="func">array_values</span><span>( </span><span class="vars">$data</span><span> ) ); </span></span></li>
<li class="alt"><span><span class="vars">$spread</span><span> = </span><span class="vars">$maximumCount</span><span> - </span><span class="vars">$minimumCount</span><span>; </span></span></li>
<li><span><span class="vars">$cloudHTML</span><span> = </span><span class="string">''</span><span>; </span></span></li>
<li class="alt"><span><span class="vars">$cloudTags</span><span> = </span><span class="keyword">array</span><span>();  </span></span></li>
<li><span> </span></li>
<li class="alt"><span><span class="vars">$spread</span><span> == 0 && </span><span class="vars">$spread</span><span> = 1;  </span></span></li>
<li><span> </span></li>
<li class="alt"><span><span class="keyword">foreach</span><span>( </span><span class="vars">$data</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$tag</span><span> => </span><span class="vars">$count</span><span> ) </span></span></li>
<li><span>{ </span></li>
<li class="alt"><span><span class="vars">$size</span><span> = </span><span class="vars">$minFontSize</span><span> + ( </span><span class="vars">$count</span><span> - </span><span class="vars">$minimumCount</span><span> ) </span></span></li>
<li><span>* ( <span class="vars">$maxFontSize</span><span> - </span><span class="vars">$minFontSize</span><span> ) / </span><span class="vars">$spread</span><span>; </span></span></li>
<li class="alt"><span><span class="vars">$cloudTags</span><span>[] = </span><span class="string">'<a><span> . </span><span class="func">floor</span><span>( </span><span class="vars">$size</span><span> ) . </span><span class="string">'px'</span><span> </span></a></span></span></li>
<li><span>. <span class="string">'" href="#" title="\''</span><span> . </span><span class="vars">$tag</span><span> . </span></span></li>
<li class="alt"><span><span class="string">'\' returned a count of '</span><span> . </span><span class="vars">$count</span><span> . </span><span class="string">'">'</span><span> </span></span></li>
<li><span>. htmlspecialchars( <span class="func">stripslashes</span><span>( </span><span class="vars">$tag</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="keyword">return</span><span> join( </span><span class="string">"\n"</span><span>, </span><span class="vars">$cloudTags</span><span> ) . </span><span class="string">"\n"</span><span>; </span></span></li>
<li><span>} </span></li>
<li class="alt"><span><span class="comment">/**************************</span> </span></li>
<li><span><span class="comment">**** Sample usage ***/</span><span> </span></span></li>
<li class="alt"><span><span class="vars">$arr</span><span> = Array(</span><span class="string">'Actionscript'</span><span> => 35, </span><span class="string">'Adobe'</span><span> => 22, </span><span class="string">'Array'</span><span> => 44, </span><span class="string">'Background'</span><span> => 43, </span></span></li>
<li><span><span class="string">'Blur'</span><span> => 18, </span><span class="string">'Canvas'</span><span> => 33, </span><span class="string">'Class'</span><span> => 15, </span><span class="string">'Color Palette'</span><span> => 11, </span><span class="string">'Crop'</span><span> => 42, </span></span></li>
<li class="alt"><span><span class="string">'Delimiter'</span><span> => 13, </span><span class="string">'Depth'</span><span> => 34, </span><span class="string">'Design'</span><span> => 8, </span><span class="string">'Encode'</span><span> => 12, </span><span class="string">'Encryption'</span><span> => 30, </span></span></li>
<li><span><span class="string">'Extract'</span><span> => 28, </span><span class="string">'Filters'</span><span> => 42); </span></span></li>
<li class="alt"><span><span class="func">echo</span><span> getCloud(</span><span class="vars">$arr</span><span>, 12, 36); </span></span></li>
</ol>
登录后复制

  13. PHP寻找两个字符串的相似性

  PHP 提供了一个极少使用的 similar_text 函数,但此函数非常有用,用于比较两个字符串并返回相似程度的百分比。

<ol class="dp-c">
<li class="alt"><span><span>similar_text(</span><span class="vars">$string1</span><span>, </span><span class="vars">$string2</span><span>, </span><span class="vars">$percent</span><span>); </span></span></li>
<li><span><span class="comment">//$percent will have the percentage of similarity</span><span> </span></span></li>
</ol>
登录后复制

  14. PHP在应用程序中使用 Gravatar 通用头像

  随着 WordPress 越来越普及,Gravatar 也随之流行。由于 Gravatar 提供了易于使用的 API,将其纳入应用程序也变得十分方便。

<ol class="dp-c">
<li class="alt"><span><span class="comment">/******************</span> </span></li>
<li><span><span class="comment">*@email - Email address to show gravatar for</span> </span></li>
<li class="alt"><span><span class="comment">*@size - size of gravatar</span> </span></li>
<li><span><span class="comment">*@default - URL of default gravatar to use</span> </span></li>
<li class="alt"><span><span class="comment">*@rating - rating of Gravatar(G, PG, R, X)</span> </span></li>
<li><span><span class="comment">*/</span><span> </span></span></li>
<li class="alt"><span><span class="keyword">function</span><span> show_gravatar(</span><span class="vars">$email</span><span>, </span><span class="vars">$size</span><span>, </span><span class="vars">$default</span><span>, </span><span class="vars">$rating</span><span>) </span></span></li>
<li><span>{ </span></li>
<li class="alt"><span><span class="func">echo</span><span> </span><span class="string">'<img  alt="再来二十一段救命的PHP代码" ><span>.md5(</span><span class="vars">$email</span><span>). </span></span></span></li>
<li><span><span class="string">'&default='</span><span>.</span><span class="vars">$default</span><span>.</span><span class="string">'&size='</span><span>.</span><span class="vars">$size</span><span>.</span><span class="string">'&rating='</span><span>.</span><span class="vars">$rating</span><span>.</span><span class="string">'" width="'</span><span>.</span><span class="vars">$size</span><span>.'px" </span></span></li>
<li class="alt"><span>height=<span class="string">"'.$size.'px"</span><span> />'; </span></span></li>
<li><span>} </span></li>
</ol>
登录后复制

  15. PHP在字符断点处截断文字

  所谓断字 (word break),即一个单词可在转行时断开的地方。这一函数将在断字处截断字符串。

<ol class="dp-c">
<li class="alt"><span><span class="comment">// Original PHP code by Chirp Internet: www.chirp.com.au</span><span> </span></span></li>
<li><span><span class="comment">// Please acknowledge use of this code by including this header.</span><span> </span></span></li>
<li class="alt"><span><span class="keyword">function</span><span> myTruncate(</span><span class="vars">$string</span><span>, </span><span class="vars">$limit</span><span>, </span><span class="vars">$break</span><span>=</span><span class="string">"."</span><span>, </span><span class="vars">$pad</span><span>=</span><span class="string">"..."</span><span>) { </span></span></li>
<li><span><span class="comment">// return with no change if string is shorter than $limit</span><span> </span></span></li>
<li class="alt"><span><span class="keyword">if</span><span>(</span><span class="func">strlen</span><span>(</span><span class="vars">$string</span><span>) <span class="vars">$limit</span><span>) </span></span></span></li>
<li><span><span class="keyword">return</span><span> </span><span class="vars">$string</span><span>;  </span></span></li>
<li class="alt"><span> </span></li>
<li><span><span class="comment">// is $break present between $limit and the end of the string?</span><span> </span></span></li>
<li class="alt"><span><span class="keyword">if</span><span>(false !== (</span><span class="vars">$breakpoint</span><span> = </span><span class="func">strpos</span><span>(</span><span class="vars">$string</span><span>, </span><span class="vars">$break</span><span>, </span><span class="vars">$limit</span><span>))) { </span></span></li>
<li><span><span class="keyword">if</span><span>(</span><span class="vars">$breakpoint</span><span> <span class="func">strlen</span><span>(</span><span class="vars">$string</span><span>) - 1) { </span></span></span></li>
<li class="alt"><span><span class="vars">$string</span><span> = </span><span class="func">substr</span><span>(</span><span class="vars">$string</span><span>, 0, </span><span class="vars">$breakpoint</span><span>) . </span><span class="vars">$pad</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="vars">$string</span><span>; </span></span></li>
<li class="alt"><span>} </span></li>
<li><span><span class="comment">/***** Example ****/</span><span> </span></span></li>
<li class="alt"><span><span class="vars">$short_string</span><span>=myTruncate(</span><span class="vars">$long_string</span><span>, 100, </span><span class="string">' '</span><span>); </span></span></li>
</ol>
登录后复制

  16. PHP文件 Zip 压缩

<ol class="dp-c">
<li class="alt"><span><span class="comment">/* creates a compressed zip file */</span><span> </span></span></li>
<li><span><span class="keyword">function</span><span> create_zip(</span><span class="vars">$files</span><span> = </span><span class="keyword">array</span><span>(),</span><span class="vars">$destination</span><span> = </span><span class="string">''</span><span>,</span><span class="vars">$overwrite</span><span> = false) { </span></span></li>
<li class="alt"><span><span class="comment">//if the zip file already exists and overwrite is false, return false</span><span> </span></span></li>
<li><span><span class="keyword">if</span><span>(</span><span class="func">file_exists</span><span>(</span><span class="vars">$destination</span><span>) && !</span><span class="vars">$overwrite</span><span>) { </span><span class="keyword">return</span><span> false; } </span></span></li>
<li class="alt"><span><span class="comment">//vars</span><span> </span></span></li>
<li><span><span class="vars">$valid_files</span><span> = </span><span class="keyword">array</span><span>(); </span></span></li>
<li class="alt"><span><span class="comment">//if files were passed in...</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">$files</span><span>)) { </span></span></li>
<li class="alt"><span><span class="comment">//cycle through each file</span><span> </span></span></li>
<li><span><span class="keyword">foreach</span><span>(</span><span class="vars">$files</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$file</span><span>) { </span></span></li>
<li class="alt"><span><span class="comment">//make sure the file exists</span><span> </span></span></li>
<li><span><span class="keyword">if</span><span>(</span><span class="func">file_exists</span><span>(</span><span class="vars">$file</span><span>)) { </span></span></li>
<li class="alt"><span><span class="vars">$valid_files</span><span>[] = </span><span class="vars">$file</span><span>; </span></span></li>
<li><span>} </span></li>
<li class="alt"><span>} </span></li>
<li><span>} </span></li>
<li class="alt"><span><span class="comment">//if we have good files...</span><span> </span></span></li>
<li><span><span class="keyword">if</span><span>(</span><span class="func">count</span><span>(</span><span class="vars">$valid_files</span><span>)) { </span></span></li>
<li class="alt"><span><span class="comment">//create the archive</span><span> </span></span></li>
<li><span><span class="vars">$zip</span><span> = </span><span class="keyword">new</span><span> ZipArchive(); </span></span></li>
<li class="alt"><span><span class="keyword">if</span><span>(</span><span class="vars">$zip</span><span>->open(</span><span class="vars">$destination</span><span>,</span><span class="vars">$overwrite</span><span> ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { </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="comment">//add the files</span><span> </span></span></li>
<li class="alt"><span><span class="keyword">foreach</span><span>(</span><span class="vars">$valid_files</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$file</span><span>) { </span></span></li>
<li><span><span class="vars">$zip</span><span>->addFile(</span><span class="vars">$file</span><span>,</span><span class="vars">$file</span><span>); </span></span></li>
<li class="alt"><span>} </span></li>
<li><span><span class="comment">//debug</span><span> </span></span></li>
<li class="alt"><span><span class="comment">//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status; </span><span> </span></span></li>
<li><span> </span></li>
<li class="alt"><span><span class="comment">//close the zip -- done!</span><span> </span></span></li>
<li><span><span class="vars">$zip</span><span>->close();  </span></span></li>
<li class="alt"><span> </span></li>
<li><span><span class="comment">//check to make sure the file exists</span><span> </span></span></li>
<li class="alt"><span><span class="keyword">return</span><span> </span><span class="func">file_exists</span><span>(</span><span class="vars">$destination</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></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="comment">/***** Example Usage ***/</span><span> </span></span></li>
<li class="alt"><span><span class="vars">$files</span><span>=</span><span class="keyword">array</span><span>(</span><span class="string">'file1.jpg'</span><span>, </span><span class="string">'file2.jpg'</span><span>, </span><span class="string">'file3.gif'</span><span>); </span></span></li>
<li><span>create_zip(<span class="vars">$files</span><span>, </span><span class="string">'myzipfile.zip'</span><span>, true); </span></span></li>
</ol>
登录后复制

  17. PHP解压缩 Zip 文件

<ol class="dp-c">
<li class="alt"><span><span class="comment">/**********************</span> </span></li>
<li><span><span class="comment">*@file - path to zip file</span> </span></li>
<li class="alt"><span><span class="comment">*@destination - destination directory for unzipped files</span> </span></li>
<li><span><span class="comment">*/</span><span> </span></span></li>
<li class="alt"><span><span class="keyword">function</span><span> unzip_file(</span><span class="vars">$file</span><span>, </span><span class="vars">$destination</span><span>){ </span></span></li>
<li><span><span class="comment">// create object</span><span> </span></span></li>
<li class="alt"><span><span class="vars">$zip</span><span> = </span><span class="keyword">new</span><span> ZipArchive() ; </span></span></li>
<li><span><span class="comment">// open archive</span><span> </span></span></li>
<li class="alt"><span><span class="keyword">if</span><span> (</span><span class="vars">$zip</span><span>->open(</span><span class="vars">$file</span><span>) !== TRUE) { </span></span></li>
<li><span><span class="keyword">die</span><span> (’Could not open archive’); </span></span></li>
<li class="alt"><span>} </span></li>
<li><span><span class="comment">// extract contents to destination directory</span><span> </span></span></li>
<li class="alt"><span><span class="vars">$zip</span><span>->extractTo(</span><span class="vars">$destination</span><span>); </span></span></li>
<li><span><span class="comment">// close archive</span><span> </span></span></li>
<li class="alt"><span><span class="vars">$zip</span><span>->close(); </span></span></li>
<li><span><span class="func">echo</span><span> </span><span class="string">'Archive extracted to directory'</span><span>; </span></span></li>
<li class="alt"><span>} </span></li>
</ol>
登录后复制

  18. PHP为 URL 地址预设 http 字符串

  有时需要接受一些表单中的网址输入,但用户很少添加 http:// 字段,此代码将为网址添加该字段。

<ol class="dp-c">
<li class="alt"><span><span class="keyword">if</span><span> (!preg_match(</span><span class="string">"/^(httpftp):/"</span><span>, </span><span class="vars">$_POST</span><span>[</span><span class="string">'url'</span><span>])) { </span></span></li>
<li><span>   <span class="vars">$_POST</span><span>[</span><span class="string">'url'</span><span>] = </span><span class="string">'http://'</span><span>.</span><span class="vars">$_POST</span><span>[</span><span class="string">'url'</span><span>]; </span></span></li>
<li class="alt"><span>} </span></li>
</ol>
登录后复制

  19. PHP将网址字符串转换成超级链接

  该函数将 URL 和 E-mail 地址字符串转换为可点击的超级链接。

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> makeClickableLinks(</span><span class="vars">$text</span><span>) { </span></span></li>
<li><span><span class="vars">$text</span><span> = </span><span class="func">eregi_replace</span><span>(</span><span class="string">'(((fht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)'</span><span>, </span></span></li>
<li class="alt"><span><span class="string">'<a>\1</a>'</span><span>, </span><span class="vars">$text</span><span>); </span></span></li>
<li><span><span class="vars">$text</span><span> = </span><span class="func">eregi_replace</span><span>(</span><span class="string">'([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)'</span><span>, </span></span></li>
<li class="alt"><span><span class="string">'\1<a>\2</a>'</span><span>, </span><span class="vars">$text</span><span>); </span></span></li>
<li><span><span class="vars">$text</span><span> = </span><span class="func">eregi_replace</span><span>(</span><span class="string">'([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})'</span><span>, </span></span></li>
<li class="alt"><span><span class="string">'<a>\1</a>'</span><span>, </span><span class="vars">$text</span><span>);  </span></span></li>
<li><span> </span></li>
<li class="alt"><span><span class="keyword">return</span><span> </span><span class="vars">$text</span><span>; </span></span></li>
<li><span>} </span></li>
</ol>
登录后复制

  20. PHP调整图像尺寸

  创建图像缩略图需要许多时间,此代码将有助于了解缩略图的逻辑。

<ol class="dp-c">
<li class="alt"><span><span class="comment">/**********************</span> </span></li>
<li><span><span class="comment">*@filename - path to the image</span> </span></li>
<li class="alt"><span><span class="comment">*@tmpname - temporary path to thumbnail</span> </span></li>
<li><span><span class="comment">*@xmax - max width</span> </span></li>
<li class="alt"><span><span class="comment">*@ymax - max height</span> </span></li>
<li><span><span class="comment">*/</span><span> </span></span></li>
<li class="alt"><span><span class="keyword">function</span><span> resize_image(</span><span class="vars">$filename</span><span>, </span><span class="vars">$tmpname</span><span>, </span><span class="vars">$xmax</span><span>, </span><span class="vars">$ymax</span><span>) </span></span></li>
<li><span>{ </span></li>
<li class="alt"><span>    <span class="vars">$ext</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">"."</span><span>, </span><span class="vars">$filename</span><span>); </span></span></li>
<li><span>    <span class="vars">$ext</span><span> = </span><span class="vars">$ext</span><span>[</span><span class="func">count</span><span>(</span><span class="vars">$ext</span><span>)-1];   </span></span></li>
<li class="alt"><span> </span></li>
<li><span>    <span class="keyword">if</span><span>(</span><span class="vars">$ext</span><span> == </span><span class="string">"jpg"</span><span>  </span><span class="vars">$ext</span><span> == </span><span class="string">"jpeg"</span><span>) </span></span></li>
<li class="alt"><span>        <span class="vars">$im</span><span> = imagecreatefromjpeg(</span><span class="vars">$tmpname</span><span>); </span></span></li>
<li><span>    <span class="keyword">elseif</span><span>(</span><span class="vars">$ext</span><span> == </span><span class="string">"png"</span><span>) </span></span></li>
<li class="alt"><span>        <span class="vars">$im</span><span> = imagecreatefrompng(</span><span class="vars">$tmpname</span><span>); </span></span></li>
<li><span>    <span class="keyword">elseif</span><span>(</span><span class="vars">$ext</span><span> == </span><span class="string">"gif"</span><span>) </span></span></li>
<li class="alt"><span>        <span class="vars">$im</span><span> = imagecreatefromgif(</span><span class="vars">$tmpname</span><span>);   </span></span></li>
<li><span> </span></li>
<li class="alt"><span>    <span class="vars">$x</span><span> = imagesx(</span><span class="vars">$im</span><span>); </span></span></li>
<li><span>    <span class="vars">$y</span><span> = imagesy(</span><span class="vars">$im</span><span>);   </span></span></li>
<li class="alt"><span> </span></li>
<li><span>    <span class="keyword">if</span><span>(</span><span class="vars">$x</span><span> <span class="vars">$xmax</span><span> && </span><span class="vars">$y</span><span> <span class="vars">$ymax</span><span>) </span></span></span></span></li>
<li class="alt"><span>        <span class="keyword">return</span><span> </span><span class="vars">$im</span><span>;   </span></span></li>
<li><span> </span></li>
<li class="alt"><span>    <span class="keyword">if</span><span>(</span><span class="vars">$x</span><span> >= </span><span class="vars">$y</span><span>) { </span></span></li>
<li><span>        <span class="vars">$newx</span><span> = </span><span class="vars">$xmax</span><span>; </span></span></li>
<li class="alt"><span>        <span class="vars">$newy</span><span> = </span><span class="vars">$newx</span><span> * </span><span class="vars">$y</span><span> / </span><span class="vars">$x</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="vars">$newy</span><span> = </span><span class="vars">$ymax</span><span>; </span></span></li>
<li class="alt"><span>        <span class="vars">$newx</span><span> = </span><span class="vars">$x</span><span> / </span><span class="vars">$y</span><span> * </span><span class="vars">$newy</span><span>; </span></span></li>
<li><span>    }   </span></li>
<li class="alt"><span> </span></li>
<li><span>    <span class="vars">$im2</span><span> = imagecreatetruecolor(</span><span class="vars">$newx</span><span>, </span><span class="vars">$newy</span><span>); </span></span></li>
<li class="alt"><span>    imagecopyresized(<span class="vars">$im2</span><span>, </span><span class="vars">$im</span><span>, 0, 0, 0, 0, </span><span class="func">floor</span><span>(</span><span class="vars">$newx</span><span>), </span><span class="func">floor</span><span>(</span><span class="vars">$newy</span><span>), </span><span class="vars">$x</span><span>, </span><span class="vars">$y</span><span>); </span></span></li>
<li><span>    <span class="keyword">return</span><span> </span><span class="vars">$im2</span><span>; </span></span></li>
<li class="alt"><span>} </span></li>
</ol>
登录后复制

  21. PHP检测 ajax 请求

  大多数的 JavaScript 框架如 jquery,Mootools 等,在发出 Ajax 请求时,都会发送额外的 HTTP_X_REQUESTED_WITH 头部信息,头当他们一个ajax请求,因此你可以在服务器端侦测到 Ajax 请求。

<ol class="dp-c">
<li class="alt"><span><span class="keyword">if</span><span>(!emptyempty(</span><span class="vars">$_SERVER</span><span>[</span><span class="string">'HTTP_X_REQUESTED_WITH'</span><span>]) && </span><span class="func">strtolower</span><span>(</span><span class="vars">$_SERVER</span><span>[</span><span class="string">'HTTP_X_REQUESTED_WITH'</span><span>]) == </span><span class="string">'xmlhttprequest'</span><span>){ </span></span></li>
<li><span>    <span class="comment">//If AJAX Request Then</span><span> </span></span></li>
<li class="alt"><span>}<span class="keyword">else</span><span>{ </span></span></li>
<li><span><span class="comment">//something else</span><span> </span></span></li>
<li class="alt"><span>} </span></li>
</ol>
登录后复制



相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板