首頁  >  文章  >  後端開發  >  超适用PHP函数总结整理

超适用PHP函数总结整理

WBOY
WBOY原創
2016-06-13 12:11:05942瀏覽

超实用PHP函数总结整理

1、PHP加密解密

PHP加密和解密函数可以用来加密一些有用的字符串存放在数据库里,并且通过可逆解密字符串,该函数使用了base64和MD5加密和解密。

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> encryptDecrypt(<span style="color: #800080;">$key</span>, <span style="color: #800080;">$string</span>, <span style="color: #800080;">$decrypt</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span>     <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$decrypt</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span>         <span style="color: #800080;">$decrypted</span> = <span style="color: #008080;">rtrim</span>(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, <span style="color: #008080;">md5</span>(<span style="color: #800080;">$key</span>), <span style="color: #008080;">base64_decode</span><span style="color: #008080;"> 6</span>        (<span style="color: #800080;">$string</span>), MCRYPT_MODE_CBC, <span style="color: #008080;">md5</span>(<span style="color: #008080;">md5</span>(<span style="color: #800080;">$key</span>))), "12"<span style="color: #000000;">); </span><span style="color: #008080;"> 7</span> <span style="color: #008080;"> 8</span>         <span style="color: #0000ff;">return</span> <span style="color: #800080;">$decrypted</span><span style="color: #000000;">; </span><span style="color: #008080;"> 9</span> <span style="color: #008080;">10</span>     }<span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #008080;">11</span> <span style="color: #008080;">12</span>         <span style="color: #800080;">$encrypted</span> = <span style="color: #008080;">base64_encode</span>(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, <span style="color: #008080;">md5</span>(<span style="color: #800080;">$key</span>),<span style="color: #008080;">13</span>        <span style="color: #800080;">$string</span>, MCRYPT_MODE_CBC, <span style="color: #008080;">md5</span>(<span style="color: #008080;">md5</span>(<span style="color: #800080;">$key</span><span style="color: #000000;">)))); </span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span>         <span style="color: #0000ff;">return</span> <span style="color: #800080;">$encrypted</span><span style="color: #000000;">; </span><span style="color: #008080;">16</span> <span style="color: #008080;">17</span> <span style="color: #000000;">    } </span><span style="color: #008080;">18</span> <span style="color: #008080;">19</span> }

使用方法如下:

<span style="color: #008080;">1</span> <span style="color: #008000;">//</span><span style="color: #008000;">以下是将字符串“Helloweba欢迎您”分别加密和解密 </span><span style="color: #008080;">2</span> <span style="color: #008080;">3</span> <span style="color: #008000;">//加密: </span><span style="color: #008080;">4</span> <span style="color: #008080;">5</span> <span style="color: #0000ff;">echo</span> encryptDecrypt('password', 'Helloweba欢迎您',0<span style="color: #000000;">); </span><span style="color: #008080;">6</span> <span style="color: #008080;">7</span> <span style="color: #008000;">//</span><span style="color: #008000;">解密: </span><span style="color: #008080;">8</span> <span style="color: #008080;">9</span> <span style="color: #0000ff;">echo</span> encryptDecrypt('password', 'z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk=',1);

2、PHP生成随机字符串

       当我们需要生成一个随机名字,临时密码等字符串时可以用到下面的函数:

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> generateRandomString(<span style="color: #800080;">$length</span> = 10<span style="color: #000000;">) { </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span>     <span style="color: #800080;">$characters</span> = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'<span style="color: #000000;">; </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span>     <span style="color: #800080;">$randomString</span> = ''<span style="color: #000000;">; </span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span>     <span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $length; <span style="color: #800080;">$i</span>++<span style="color: #000000;">) { </span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span>         <span style="color: #800080;">$randomString</span> .= <span style="color: #800080;">$characters</span>[<span style="color: #008080;">rand</span>(0, <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$characters</span>) - 1<span style="color: #000000;">)]; </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span> <span style="color: #000000;">    } </span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span>     <span style="color: #0000ff;">return</span> <span style="color: #800080;">$randomString</span><span style="color: #000000;">; </span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span> }

  使用方法如下:

<span style="color: #008080;">1</span> <span style="color: #0000ff;">echo</span> generateRandomString(20);

3、PHP获取文件扩展名(后缀)

 以下函数可以快速获取文件的扩展名即后缀。

<span style="color: #008080;">1</span> <span style="color: #0000ff;">function</span> getExtension(<span style="color: #800080;">$filename</span><span style="color: #000000;">){ </span><span style="color: #008080;">2</span> <span style="color: #008080;">3</span>   <span style="color: #800080;">$myext</span> = <span style="color: #008080;">substr</span>(<span style="color: #800080;">$filename</span>, <span style="color: #008080;">strrpos</span>(<span style="color: #800080;">$filename</span>, '.'<span style="color: #000000;">)); </span><span style="color: #008080;">4</span> <span style="color: #008080;">5</span>   <span style="color: #0000ff;">return</span> <span style="color: #008080;">str_replace</span>('.','',<span style="color: #800080;">$myext</span><span style="color: #000000;">); </span><span style="color: #008080;">6</span> <span style="color: #008080;">7</span> }

使用方法如下:

<span style="color: #008080;">1</span> <span style="color: #800080;">$filename</span> = '我的文档.doc'<span style="color: #000000;">; </span><span style="color: #008080;">2</span> <span style="color: #008080;">3</span> <span style="color: #0000ff;">echo</span> getExtension(<span style="color: #800080;">$filename</span>);

4、PHP获取文件大小并格式化

以下使用的函数可以获取文件的大小,并且转换成便于阅读的KB,MB等格式。

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> formatSize(<span style="color: #800080;">$size</span><span style="color: #000000;">) { </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span>     <span style="color: #800080;">$sizes</span> = <span style="color: #0000ff;">array</span>(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"<span style="color: #000000;">); </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span>     <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$size</span> == 0<span style="color: #000000;">) {  </span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span>         <span style="color: #0000ff;">return</span>('n/a'<span style="color: #000000;">);  </span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span>     } <span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span>       <span style="color: #0000ff;">return</span> (<span style="color: #008080;">round</span>(<span style="color: #800080;">$size</span>/<span style="color: #008080;">pow</span>(1024, (<span style="color: #800080;">$i</span> = <span style="color: #008080;">floor</span>(<span style="color: #008080;">log</span>(<span style="color: #800080;">$size</span>, 1024)))), 2) . <span style="color: #800080;">$sizes</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">]);  </span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span> <span style="color: #000000;">    } </span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span> }

使用方法如下:

<span style="color: #008080;">1</span> <span style="color: #800080;">$thefile</span> = <span style="color: #008080;">filesize</span>('test_file.mp3'<span style="color: #000000;">); </span><span style="color: #008080;">2</span> <span style="color: #008080;">3</span> <span style="color: #0000ff;">echo</span> formatSize(<span style="color: #800080;">$thefile</span>);

5、PHP替换标签字符

有时我们需要将字符串、模板标签替换成指定的内容,可以用到下面的函数:

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> stringParser(<span style="color: #800080;">$string</span>,<span style="color: #800080;">$replacer</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span>     <span style="color: #800080;">$result</span> = <span style="color: #008080;">str_replace</span>(<span style="color: #008080;">array_keys</span>(<span style="color: #800080;">$replacer</span>), <span style="color: #008080;">array_values</span>(<span style="color: #800080;">$replacer</span>),<span style="color: #800080;">$string</span><span style="color: #000000;">); </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span>     <span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span><span style="color: #000000;">; </span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span> <span style="color: #000000;">}</span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span> <span style="color: #008000;">//</span><span style="color: #008000;">使用方法如下</span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span> <span style="color: #800080;">$string</span> = '<span style="color: #000000;">The {b}anchor text{/b} is the {b}actual word{/b} or words used </span><span style="color: #008080;">12</span> {br}to describe the link {br}itself'<span style="color: #000000;">; </span><span style="color: #008080;">13</span> <span style="color: #008080;">14</span> <span style="color: #800080;">$replace_array</span> = <span style="color: #0000ff;">array</span>('{b}' => '<b>','{/b}' => '</b>','{br}' => '<br>'<span style="color: #000000;">); </span><span style="color: #008080;">15</span> <span style="color: #008080;">16</span>  <span style="color: #008080;">17</span> <span style="color: #008080;">18</span> <span style="color: #0000ff;">echo</span> stringParser(<span style="color: #800080;">$string</span>,<span style="color: #800080;">$replace_array</span>);

6、PHP列出目录下的文件名

如果你想列出目录下的所有文件,使用以下代码即可:

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> listDirFiles(<span style="color: #800080;">$DirPath</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span>     <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$dir</span> = <span style="color: #008080;">opendir</span>(<span style="color: #800080;">$DirPath</span><span style="color: #000000;">)){ </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span>          <span style="color: #0000ff;">while</span>((<span style="color: #800080;">$file</span> = <span style="color: #008080;">readdir</span>(<span style="color: #800080;">$dir</span>))!== <span style="color: #0000ff;">false</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span>                 <span style="color: #0000ff;">if</span>(!<span style="color: #008080;">is_dir</span>(<span style="color: #800080;">$DirPath</span>.<span style="color: #800080;">$file</span><span style="color: #000000;">)) </span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span> <span style="color: #000000;">                { </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span>                     <span style="color: #0000ff;">echo</span> "filename: <span style="color: #800080;">$file</span><br>"<span style="color: #000000;">; </span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span> <span style="color: #000000;">                } </span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span> <span style="color: #000000;">         } </span><span style="color: #008080;">16</span> <span style="color: #008080;">17</span> <span style="color: #000000;">    } </span><span style="color: #008080;">18</span> <span style="color: #008080;">19</span> <span style="color: #000000;">}</span><span style="color: #008080;">20</span> <span style="color: #008080;">21</span> <span style="color: #008000;">//</span><span style="color: #008000;">使用方法如下</span><span style="color: #008080;">22</span> <span style="color: #008080;">23</span> listDirFiles('home/some_folder/');

7、PHP获取当前页面URL

以下函数可以获取当前页面的URL,不管是http还是https。

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> curPageURL() { </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span>     <span style="color: #800080;">$pageURL</span> = 'http'<span style="color: #000000;">; </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span>     <span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_SERVER</span>['HTTPS'])) {<span style="color: #800080;">$pageURL</span> .= "s"<span style="color: #000000;">;} </span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span>     <span style="color: #800080;">$pageURL</span> .= "://"<span style="color: #000000;">; </span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span>     <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$_SERVER</span>["SERVER_PORT"] != "80"<span style="color: #000000;">) { </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span>         <span style="color: #800080;">$pageURL</span> .= <span style="color: #800080;">$_SERVER</span>["SERVER_NAME"].":".<span style="color: #800080;">$_SERVER</span>["SERVER_PORT"].<span style="color: #800080;">$_SERVER</span><span style="color: #008080;">12</span> ["REQUEST_URI"<span style="color: #000000;">]; </span><span style="color: #008080;">13</span> <span style="color: #008080;">14</span>     } <span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008080;">15</span> <span style="color: #008080;">16</span>         <span style="color: #800080;">$pageURL</span> .= <span style="color: #800080;">$_SERVER</span>["SERVER_NAME"].<span style="color: #800080;">$_SERVER</span>["REQUEST_URI"<span style="color: #000000;">]; </span><span style="color: #008080;">17</span> <span style="color: #008080;">18</span> <span style="color: #000000;">    } </span><span style="color: #008080;">19</span> <span style="color: #008080;">20</span>     <span style="color: #0000ff;">return</span> <span style="color: #800080;">$pageURL</span><span style="color: #000000;">; </span><span style="color: #008080;">21</span> <span style="color: #008080;">22</span> <span style="color: #000000;">}</span><span style="color: #008080;">23</span> <span style="color: #008080;">24</span> <span style="color: #008000;">//</span><span style="color: #008000;">使用方法如下</span><span style="color: #008080;">25</span> <span style="color: #008080;">26</span> <span style="color: #0000ff;">echo</span> curPageURL();

8、PHP强制下载文件

有时我们不想让浏览器直接打开文件,如PDF文件,而是要直接下载文件,那么以下函数可以强制下载文件,函数中使用了application/octet-stream头类型。

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> download(<span style="color: #800080;">$filename</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span>     <span style="color: #0000ff;">if</span> ((<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$filename</span>))&&(<span style="color: #008080;">file_exists</span>(<span style="color: #800080;">$filename</span><span style="color: #000000;">))){ </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span>        <span style="color: #008080;">header</span>("Content-length: ".<span style="color: #008080;">filesize</span>(<span style="color: #800080;">$filename</span><span style="color: #000000;">)); </span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span>        <span style="color: #008080;">header</span>('Content-Type: application/octet-stream'<span style="color: #000000;">); </span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span>        <span style="color: #008080;">header</span>('Content-Disposition: attachment; filename="' . <span style="color: #800080;">$filename</span> . '"'<span style="color: #000000;">); </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span>        <span style="color: #008080;">readfile</span>("<span style="color: #800080;">$filename</span>"<span style="color: #000000;">); </span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span>     } <span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span>        <span style="color: #0000ff;">echo</span> "Looks like file does not exist!"<span style="color: #000000;">; </span><span style="color: #008080;">16</span> <span style="color: #008080;">17</span> <span style="color: #000000;">    } </span><span style="color: #008080;">18</span> <span style="color: #008080;">19</span> <span style="color: #000000;">}</span><span style="color: #008080;">20</span> <span style="color: #008080;">21</span> <span style="color: #008000;">//</span><span style="color: #008000;">使用方法如下</span><span style="color: #008080;">22</span> <span style="color: #008080;">23</span> download('/down/test_45f73e852.zip'); 

9、PHP截取字符串长度

我们经常会遇到需要截取字符串(含中文汉字)长度的情况,比如标题显示不能超过多少字符,超出的长度用…表示,以下函数可以满足你的需求。

<span style="color: #008080;"> 1</span> <span style="color: #008000;">/*</span> <span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #008000;"> Utf-8、gb2312都支持的汉字截取函数 </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span> <span style="color: #008000;"> cut_str(字符串, 截取长度, 开始长度, 编码); </span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span> <span style="color: #008000;"> 编码默认为 utf-8 </span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span> <span style="color: #008000;"> 开始长度默认为 0 </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span> <span style="color: #008000;">*/</span> <span style="color: #008080;">12</span> <span style="color: #008080;">13</span> <span style="color: #0000ff;">function</span> cutStr(<span style="color: #800080;">$string</span>, <span style="color: #800080;">$sublen</span>, <span style="color: #800080;">$start</span> = 0, <span style="color: #800080;">$code</span> = 'UTF-8'<span style="color: #000000;">){ </span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span>     <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$code</span> == 'UTF-8'<span style="color: #000000;">){ </span><span style="color: #008080;">16</span> <span style="color: #008080;">17</span>         <span style="color: #800080;">$pa</span> = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"<span style="color: #000000;">; </span><span style="color: #008080;">18</span> <span style="color: #008080;">19</span>         <span style="color: #008080;">preg_match_all</span>(<span style="color: #800080;">$pa</span>, <span style="color: #800080;">$string</span>, <span style="color: #800080;">$t_string</span><span style="color: #000000;">); </span><span style="color: #008080;">20</span> <span style="color: #008080;">21</span>  <span style="color: #008080;">22</span> <span style="color: #008080;">23</span>         <span style="color: #0000ff;">if</span>(<span style="color: #008080;">count</span>(<span style="color: #800080;">$t_string</span>[0]) - <span style="color: #800080;">$start</span> > <span style="color: #800080;">$sublen</span>) <span style="color: #0000ff;">return</span> <span style="color: #008080;">join</span>('', <span style="color: #008080;">array_slice</span><span style="color: #008080;">24</span> (<span style="color: #800080;">$t_string</span>[0], <span style="color: #800080;">$start</span>, <span style="color: #800080;">$sublen</span>))."..."<span style="color: #000000;">; </span><span style="color: #008080;">25</span> <span style="color: #008080;">26</span>         <span style="color: #0000ff;">return</span> <span style="color: #008080;">join</span>('', <span style="color: #008080;">array_slice</span>(<span style="color: #800080;">$t_string</span>[0], <span style="color: #800080;">$start</span>, <span style="color: #800080;">$sublen</span><span style="color: #000000;">)); </span><span style="color: #008080;">27</span> <span style="color: #008080;">28</span>     }<span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #008080;">29</span> <span style="color: #008080;">30</span>         <span style="color: #800080;">$start</span> = <span style="color: #800080;">$start</span>*2<span style="color: #000000;">; </span><span style="color: #008080;">31</span> <span style="color: #008080;">32</span>         <span style="color: #800080;">$sublen</span> = <span style="color: #800080;">$sublen</span>*2<span style="color: #000000;">; </span><span style="color: #008080;">33</span> <span style="color: #008080;">34</span>         <span style="color: #800080;">$strlen</span> = <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$string</span><span style="color: #000000;">); </span><span style="color: #008080;">35</span> <span style="color: #008080;">36</span>         <span style="color: #800080;">$tmpstr</span> = ''<span style="color: #000000;">; </span><span style="color: #008080;">37</span> <span style="color: #008080;">38</span>  <span style="color: #008080;">39</span> <span style="color: #008080;">40</span>         <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0; <span style="color: #800080;">$i</span>$strlen; <span style="color: #800080;">$i</span>++<span style="color: #000000;">){ </span><span style="color: #008080;">41</span> <span style="color: #008080;">42</span>             <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$i</span>>=<span style="color: #800080;">$start</span> && <span style="color: #800080;">$i</span>$start+<span style="color: #800080;">$sublen</span><span style="color: #000000;">)){ </span><span style="color: #008080;">43</span> <span style="color: #008080;">44</span>                 <span style="color: #0000ff;">if</span>(<span style="color: #008080;">ord</span>(<span style="color: #008080;">substr</span>(<span style="color: #800080;">$string</span>, <span style="color: #800080;">$i</span>, 1))>129<span style="color: #000000;">){ </span><span style="color: #008080;">45</span> <span style="color: #008080;">46</span>                     <span style="color: #800080;">$tmpstr</span>.= <span style="color: #008080;">substr</span>(<span style="color: #800080;">$string</span>, <span style="color: #800080;">$i</span>, 2<span style="color: #000000;">); </span><span style="color: #008080;">47</span> <span style="color: #008080;">48</span>                 }<span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #008080;">49</span> <span style="color: #008080;">50</span>                     <span style="color: #800080;">$tmpstr</span>.= <span style="color: #008080;">substr</span>(<span style="color: #800080;">$string</span>, <span style="color: #800080;">$i</span>, 1<span style="color: #000000;">); </span><span style="color: #008080;">51</span> <span style="color: #008080;">52</span> <span style="color: #000000;">                } </span><span style="color: #008080;">53</span> <span style="color: #008080;">54</span> <span style="color: #000000;">            } </span><span style="color: #008080;">55</span> <span style="color: #008080;">56</span>             <span style="color: #0000ff;">if</span>(<span style="color: #008080;">ord</span>(<span style="color: #008080;">substr</span>(<span style="color: #800080;">$string</span>, <span style="color: #800080;">$i</span>, 1))>129) <span style="color: #800080;">$i</span>++<span style="color: #000000;">; </span><span style="color: #008080;">57</span> <span style="color: #008080;">58</span> <span style="color: #000000;">        } </span><span style="color: #008080;">59</span> <span style="color: #008080;">60</span>         <span style="color: #0000ff;">if</span>(<span style="color: #008080;">strlen</span>(<span style="color: #800080;">$tmpstr</span>)$strlen ) <span style="color: #800080;">$tmpstr</span>.= "..."<span style="color: #000000;">; </span><span style="color: #008080;">61</span> <span style="color: #008080;">62</span>         <span style="color: #0000ff;">return</span> <span style="color: #800080;">$tmpstr</span><span style="color: #000000;">; </span><span style="color: #008080;">63</span> <span style="color: #008080;">64</span> <span style="color: #000000;">    } </span><span style="color: #008080;">65</span> <span style="color: #008080;">66</span> <span style="color: #000000;">}</span><span style="color: #008080;">67</span> <span style="color: #008080;">68</span> <span style="color: #008000;">//</span><span style="color: #008000;">使用方法如下</span><span style="color: #008080;">69</span> <span style="color: #008080;">70</span> <span style="color: #800080;">$str</span> = "jQuery插件实现的加载图片和页面效果"<span style="color: #000000;">; </span><span style="color: #008080;">71</span> <span style="color: #008080;">72</span> <span style="color: #0000ff;">echo</span> cutStr(<span style="color: #800080;">$str</span>,16);

10、PHP获取客户端真实IP

我们经常要用数据库记录用户的IP,以下代码可以获取客户端真实的IP:

<span style="color: #008080;"> 1</span> <span style="color: #008000;">//</span><span style="color: #008000;">获取用户真实IP </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getIp() { </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span>     <span style="color: #0000ff;">if</span> (<span style="color: #008080;">getenv</span>("HTTP_CLIENT_IP") && <span style="color: #008080;">strcasecmp</span>(<span style="color: #008080;">getenv</span>("HTTP_CLIENT_IP"),<span style="color: #008080;"> 6</span>  "unknown"<span style="color: #000000;">)) </span><span style="color: #008080;"> 7</span> <span style="color: #008080;"> 8</span>         <span style="color: #800080;">$ip</span> = <span style="color: #008080;">getenv</span>("HTTP_CLIENT_IP"<span style="color: #000000;">); </span><span style="color: #008080;"> 9</span> <span style="color: #008080;">10</span>     <span style="color: #0000ff;">else</span> <span style="color: #008080;">11</span> <span style="color: #008080;">12</span>         <span style="color: #0000ff;">if</span> (<span style="color: #008080;">getenv</span>("HTTP_X_FORWARDED_FOR") && <span style="color: #008080;">strcasecmp</span>(<span style="color: #008080;">getenv</span>("HTTP_X_FORWARDED_FOR"), "unknown"<span style="color: #000000;">)) </span><span style="color: #008080;">13</span> <span style="color: #008080;">14</span>             <span style="color: #800080;">$ip</span> = <span style="color: #008080;">getenv</span>("HTTP_X_FORWARDED_FOR"<span style="color: #000000;">); </span><span style="color: #008080;">15</span> <span style="color: #008080;">16</span>         <span style="color: #0000ff;">else</span> <span style="color: #008080;">17</span> <span style="color: #008080;">18</span>             <span style="color: #0000ff;">if</span> (<span style="color: #008080;">getenv</span>("REMOTE_ADDR") && <span style="color: #008080;">strcasecmp</span>(<span style="color: #008080;">getenv</span>("REMOTE_ADDR"), "unknown"<span style="color: #000000;">)) </span><span style="color: #008080;">19</span> <span style="color: #008080;">20</span>                 <span style="color: #800080;">$ip</span> = <span style="color: #008080;">getenv</span>("REMOTE_ADDR"<span style="color: #000000;">); </span><span style="color: #008080;">21</span> <span style="color: #008080;">22</span>             <span style="color: #0000ff;">else</span> <span style="color: #008080;">23</span> <span style="color: #008080;">24</span>                 <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span> (<span style="color: #800080;">$_SERVER</span>['REMOTE_ADDR']) && <span style="color: #800080;">$_SERVER</span>['REMOTE_ADDR'] && <span style="color: #008080;">25</span> <span style="color: #008080;">strcasecmp</span>(<span style="color: #800080;">$_SERVER</span>['REMOTE_ADDR'], "unknown"<span style="color: #000000;">)) </span><span style="color: #008080;">26</span> <span style="color: #008080;">27</span>                     <span style="color: #800080;">$ip</span> = <span style="color: #800080;">$_SERVER</span>['REMOTE_ADDR'<span style="color: #000000;">]; </span><span style="color: #008080;">28</span> <span style="color: #008080;">29</span>                 <span style="color: #0000ff;">else</span> <span style="color: #008080;">30</span> <span style="color: #008080;">31</span>                     <span style="color: #800080;">$ip</span> = "unknown"<span style="color: #000000;">; </span><span style="color: #008080;">32</span> <span style="color: #008080;">33</span>     <span style="color: #0000ff;">return</span> (<span style="color: #800080;">$ip</span><span style="color: #000000;">); </span><span style="color: #008080;">34</span> <span style="color: #008080;">35</span> <span style="color: #000000;">}</span><span style="color: #008080;">36</span> <span style="color: #008080;">37</span> <span style="color: #008000;">//</span><span style="color: #008000;">使用方法如下</span><span style="color: #008080;">38</span> <span style="color: #008080;">39</span> <span style="color: #0000ff;">echo</span> getIp();

11、PHP防止SQL注入

我们在查询数据库时,出于安全考虑,需要过滤一些非法字符防止SQL恶意注入,请看一下函数:

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> injCheck(<span style="color: #800080;">$sql_str</span><span style="color: #000000;">) {  </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span>     <span style="color: #800080;">$check</span> = <span style="color: #008080;">preg_match</span>('<span style="color: #000000;">/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into</span><span style="color: #008080;"> 4</span> |load_file|outfile/', <span style="color: #800080;">$sql_str</span><span style="color: #000000;">); </span><span style="color: #008080;"> 5</span> <span style="color: #008080;"> 6</span>     <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$check</span><span style="color: #000000;">) { </span><span style="color: #008080;"> 7</span> <span style="color: #008080;"> 8</span>         <span style="color: #0000ff;">echo</span> '非法字符!!'<span style="color: #000000;">; </span><span style="color: #008080;"> 9</span> <span style="color: #008080;">10</span>         <span style="color: #0000ff;">exit</span><span style="color: #000000;">; </span><span style="color: #008080;">11</span> <span style="color: #008080;">12</span>     } <span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008080;">13</span> <span style="color: #008080;">14</span>         <span style="color: #0000ff;">return</span> <span style="color: #800080;">$sql_str</span><span style="color: #000000;">; </span><span style="color: #008080;">15</span> <span style="color: #008080;">16</span> <span style="color: #000000;">    } </span><span style="color: #008080;">17</span> <span style="color: #008080;">18</span> <span style="color: #000000;">}</span><span style="color: #008080;">19</span> <span style="color: #008080;">20</span> <span style="color: #008000;">//</span><span style="color: #008000;">使用方法如下</span><span style="color: #008080;">21</span> <span style="color: #008080;">22</span> <span style="color: #0000ff;">echo</span> injCheck('1 or 1=1');

12、PHP页面提示与跳转

我们在进行表单操作时,有时为了友好需要提示用户操作结果,并跳转到相关页面,请看以下函数:

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> message(<span style="color: #800080;">$msgTitle</span>,<span style="color: #800080;">$message</span>,<span style="color: #800080;">$jumpUrl</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 2</span>     <span style="color: #800080;">$str</span> = ''<span style="color: #000000;">; </span><span style="color: #008080;"> 3</span>     <span style="color: #800080;">$str</span> .= ''<span style="color: #000000;">; </span><span style="color: #008080;"> 4</span>     <span style="color: #800080;">$str</span> .= ''<span style="color: #000000;">; </span><span style="color: #008080;"> 5</span>     <span style="color: #800080;">$str</span> .= '<meta charset="utf-8">'<span style="color: #000000;">; </span><span style="color: #008080;"> 6</span>     <span style="color: #800080;">$str</span> .= '<title>页面提示</title>'<span style="color: #000000;">; </span><span style="color: #008080;"> 7</span>     <span style="color: #800080;">$str</span> .= '<style type="text/css">'<span style="color: #000000;">; <span style="color: #008080;"> 8     <span style="color: #800080;">$str .= '<span style="color: #000000;">*{margin:0; padding:0}a{color:#369; text-decoration:none;}a:hover{text-decoration:underline}body{height:100%; font:12px/18px Tahoma, Arial,  sans-serif; color:#424242; background:#fff}.message{width:450px; height:120px; <span style="color: #008080;"> 9 <span style="color: #000000;">margin:16% auto; border:1px solid #99b1c4; background:#ecf7fb}.message h3{height:28px; <span style="color: #008080;">10 line-height:28px; background:#2c91c6; text-align:center; color:#fff; font-size:14px}.msg_txt{padding:10px; margin-top:8px}.msg_txt h4{line-height:26px; font-size:14px}.msg_txt h4.red{color:#f30}.msg_txt p{line-height:22px}'<span style="color: #000000;">; <span style="color: #008080;">11     <span style="color: #800080;">$str .= '</style>'<span style="color: #000000;">; </span><span style="color: #008080;">12</span>     <span style="color: #800080;">$str</span> .= ''<span style="color: #000000;">; </span><span style="color: #008080;">13</span>     <span style="color: #800080;">$str</span> .= ''<span style="color: #000000;">; </span><span style="color: #008080;">14</span>     <span style="color: #800080;">$str</span> .= '<div>'<span style="color: #000000;">; </span><span style="color: #008080;">15</span>     <span style="color: #800080;">$str</span> .= '<h3>'.<span style="color: #800080;">$msgTitle</span>.'</h3>'<span style="color: #000000;">; </span><span style="color: #008080;">16</span>     <span style="color: #800080;">$str</span> .= '<div>'<span style="color: #000000;">; </span><span style="color: #008080;">17</span>     <span style="color: #800080;">$str</span> .= '<h4>'.<span style="color: #800080;">$message</span>.'</h4>'<span style="color: #000000;">; </span><span style="color: #008080;">18</span>     <span style="color: #800080;">$str</span> .= '<p>系统将在 <span style="color:blue;font-weight:bold">3</span> 秒后自动跳转,如果不想等待,直接点击 <a href="%7B%24jumpUrl%7D">这里</a> 跳转</p>'<span style="color: #000000;">; </span><span style="color: #008080;">19</span>     <span style="color: #800080;">$str</span> .= "<script>setTimeout('location.replace(\'".<span style="color: #800080;">$jumpUrl."\')',2000)</script>"<span style="color: #000000;">; </span><span style="color: #008080;">20</span>     <span style="color: #800080;">$str</span> .= '</div>'<span style="color: #000000;">; </span><span style="color: #008080;">21</span>     <span style="color: #800080;">$str</span> .= '</div>'<span style="color: #000000;">; </span><span style="color: #008080;">22</span>     <span style="color: #800080;">$str</span> .= ''<span style="color: #000000;">; </span><span style="color: #008080;">23</span>     <span style="color: #800080;">$str</span> .= ''<span style="color: #000000;">; </span><span style="color: #008080;">24</span>     <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$str</span><span style="color: #000000;">; </span><span style="color: #008080;">25</span> <span style="color: #000000;">}</span><span style="color: #008080;">26</span> <span style="color: #008080;">27</span> <span style="color: #008000;">//</span><span style="color: #008000;">使用方法如下 </span><span style="color: #008080;">28</span> <span style="color: #008080;">29</span> message('操作提示','操作成功!','http://www.helloweba.com/');

13、PHP计算时长

我们在处理时间时,需要计算当前时间距离某个时间点的时长,如计算客户端运行时长,通常用hh:mm:ss表示。

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> changeTimeType(<span style="color: #800080;">$seconds</span><span style="color: #000000;">) { </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span>     <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$seconds</span> > 3600<span style="color: #000000;">) { </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span>         <span style="color: #800080;">$hours</span> = <span style="color: #008080;">intval</span>(<span style="color: #800080;">$seconds</span> / 3600<span style="color: #000000;">); </span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span>         <span style="color: #800080;">$minutes</span> = <span style="color: #800080;">$seconds</span> % 3600<span style="color: #000000;">; </span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span>         <span style="color: #800080;">$time</span> = <span style="color: #800080;">$hours</span> . ":" . <span style="color: #008080;">gmstrftime</span>('%M:%S', <span style="color: #800080;">$minutes</span><span style="color: #000000;">); </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span>     } <span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span>         <span style="color: #800080;">$time</span> = <span style="color: #008080;">gmstrftime</span>('%H:%M:%S', <span style="color: #800080;">$seconds</span><span style="color: #000000;">); </span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span> <span style="color: #000000;">    } </span><span style="color: #008080;">16</span> <span style="color: #008080;">17</span>     <span style="color: #0000ff;">return</span> <span style="color: #800080;">$time</span><span style="color: #000000;">; </span><span style="color: #008080;">18</span> <span style="color: #008080;">19</span> <span style="color: #000000;">}</span><span style="color: #008080;">20</span> <span style="color: #008080;">21</span> <span style="color: #008000;">//</span><span style="color: #008000;">使用方法如下</span><span style="color: #008080;">22</span> <span style="color: #008080;">23</span> <span style="color: #800080;">$seconds</span> = 3712<span style="color: #000000;">; </span><span style="color: #008080;">24</span> <span style="color: #008080;">25</span> <span style="color: #0000ff;">echo</span> changeTimeType(<span style="color: #800080;">$seconds</span>);

 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn