search
Homephp教程php手册超实用PHP函数总结整理,实用php函数总结

超实用PHP函数总结整理,实用php函数总结

1、PHP加密解密

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

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

使用方法如下:

<span>1</span> <span>//</span><span>以下是将字符串&ldquo;Helloweba欢迎您&rdquo;分别加密和解密 
</span><span>2</span> 
<span>3</span> <span>//加密: </span>
<span>4</span> 
<span>5</span> <span>echo</span> encryptDecrypt('password', 'Helloweba欢迎您',0<span>); 
</span><span>6</span> 
<span>7</span> <span>//</span><span>解密: </span>
<span>8</span> 
<span>9</span> <span>echo</span> encryptDecrypt('password', 'z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk=',1);

2、PHP生成随机字符串

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

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

  使用方法如下:

<span>1</span> <span>echo</span> generateRandomString(20);

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

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

<span>1</span> <span>function</span> getExtension(<span>$filename</span><span>){ 
</span><span>2</span> 
<span>3</span>   <span>$myext</span> = <span>substr</span>(<span>$filename</span>, <span>strrpos</span>(<span>$filename</span>, '.'<span>)); 
</span><span>4</span> 
<span>5</span>   <span>return</span> <span>str_replace</span>('.','',<span>$myext</span><span>); 
</span><span>6</span> 
<span>7</span> }

使用方法如下:

<span>1</span> <span>$filename</span> = '我的文档.doc'<span>; 
</span><span>2</span> 
<span>3</span> <span>echo</span> getExtension(<span>$filename</span>);

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

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

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

使用方法如下:

<span>1</span> <span>$thefile</span> = <span>filesize</span>('test_file.mp3'<span>); 
</span><span>2</span> 
<span>3</span> <span>echo</span> formatSize(<span>$thefile</span>);

5、PHP替换标签字符

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

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

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

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

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

7、PHP获取当前页面URL

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

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

8、PHP强制下载文件

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

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

9、PHP截取字符串长度

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

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

10、PHP获取客户端真实IP

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

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

11、PHP防止SQL注入

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

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

12、PHP页面提示与跳转

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

<span> 1</span> <span>function</span> message(<span>$msgTitle</span>,<span>$message</span>,<span>$jumpUrl</span><span>){ 
</span><span> 2</span>     <span>$str</span> = '<!DOCTYPE HTML>'<span>; 
</span><span> 3</span>     <span>$str</span> .= '<html>'<span>; 
</span><span> 4</span>     <span>$str</span> .= '<head>'<span>; 
</span><span> 5</span>     <span>$str</span> .= '<meta charset="utf-8">'<span>; 
</span><span> 6</span>     <span>$str</span> .= '<title>页面提示</title>'<span>; 
</span><span> 7</span>     <span>$str</span> .= '<style type="text/css">'<span>; 
</span><span> 8</span>     <span>$str</span> .= '<span>*{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><span> 9</span> <span>margin:16% auto; border:1px solid #99b1c4; background:#ecf7fb}.message h3{height:28px; 
</span><span>10</span> 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>; 
</span><span>11</span>     <span>$str</span> .= '</style>'<span>; 
</span><span>12</span>     <span>$str</span> .= '</head>'<span>; 
</span><span>13</span>     <span>$str</span> .= '<body>'<span>; 
</span><span>14</span>     <span>$str</span> .= '<div>'<span>; 
</span><span>15</span>     <span>$str</span> .= '<h3 id="span-msgTitle-span">'.<span>$msgTitle</span>.'</h3>'<span>; 
</span><span>16</span>     <span>$str</span> .= '<div>'<span>; 
</span><span>17</span>     <span>$str</span> .= '<h4 id="span-message-span">'.<span>$message</span>.'</h4>'<span>; 
</span><span>18</span>     <span>$str</span> .= '<p>系统将在 <span>; 
</span><span>19</span>     <span>$str</span> .= "<script>setTimeout('location.replace(\'".<span>$jumpUrl</span>."\')',2000)</script>"<span>; 
</span><span>20</span>     <span>$str</span> .= '</div>'<span>; 
</span><span>21</span>     <span>$str</span> .= '</div>'<span>; 
</span><span>22</span>     <span>$str</span> .= '</body>'<span>; 
</span><span>23</span>     <span>$str</span> .= '</html>'<span>; 
</span><span>24</span>     <span>echo</span> <span>$str</span><span>; 
</span><span>25</span> <span>}
</span><span>26</span> 
<span>27</span> <span>//</span><span>使用方法如下 </span>
<span>28</span> 
<span>29</span> message('操作提示','操作成功!','http://www.helloweba.com/');

13、PHP计算时长

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

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

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么去除字符串中的所有大写字母php怎么去除字符串中的所有大写字母Sep 26, 2022 pm 07:59 PM

两种去除方法:1、利用preg_replace()执行正则表达式搜索所有大写字母并将其替换为空字符即可,语法“preg_replace('/[A-Z]/','',$str)”。2、利用preg_filter()执行正则表达式搜索所有大写字母并将其替换为空字符即可,语法“preg_filter('/[A-Z]/','',$str)”。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

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

使用Go语言编写高性能的正则表达式匹配使用Go语言编写高性能的正则表达式匹配Jun 15, 2023 pm 10:56 PM

随着数据量的不断增大,正则表达式匹配成为了程序中常用的操作之一。而在Go语言中,由于其天然的并行ism,以及与底层系统的交互性和高效性,使得Go语言的正则表达式匹配极具优势。那么如何使用Go语言编写高性能的正则表达式匹配呢?一、了解正则表达式在使用正则表达式前,我们首先需要了解正则表达式,了解其基本语法规则以及常用的匹配字符,使我们能够在编写正则表达式时更加

php怎么利用正则排除字符串中的字符php怎么利用正则排除字符串中的字符Dec 15, 2022 pm 03:30 PM

两种方法:1、用preg_replace(),可执行正则表达式的搜索和替换,只需将字符串中匹配的字符替换为空字符即可,语法“preg_replace(正则, "", $str)”。2、用preg_match_all(),可搜索字符串中所有和正则表达式匹配的结果,会将每次的匹配结果放在一个数组$array中,语法“preg_match_all(正则,$str,$array);”。

php怎么只获取中文字符php怎么只获取中文字符Apr 28, 2022 pm 08:15 PM

php中可用preg_match_all()配合正则表达式过滤字符串,只获取中文字符;语法“preg_match_all("/[\x{4e00}-\x{9fff}]+/u","$str",$arr);”,会将匹配字符存入“$arr”数组中。

javascript怎么正则替换非汉字的字符javascript怎么正则替换非汉字的字符Oct 13, 2022 pm 05:37 PM

在javascript中,可以使用replace()函数配合正则表达式“/[u4e00-u9fa5|,]+/ig”来查找字符串中的所有非汉字字符,并将其替换为其他指定值,语法“字符串对象.replace(/[u4e00-u9fa5|,]+/ig,'指定替换值')”。

Java语言正则表达式的使用方法Java语言正则表达式的使用方法Jun 10, 2023 am 08:13 AM

Java语言正则表达式的使用方法正则表达式是一种强大的文本处理工具,可以用来匹配和验证文本。在Java语言中,也可以使用正则表达式来实现字符串的匹配和处理。本文将介绍Java语言正则表达式的使用方法,涵盖正则表达式的基础知识,常用的正则表达式语法,以及在Java程序中使用正则表达式的方法。一、基础知识正则表达式是什么?正则表达式是一种文本模式,用来描述一组字

PHP开发:如何编写高效的正则表达式PHP开发:如何编写高效的正则表达式Jun 15, 2023 pm 09:04 PM

在PHP开发中,正则表达式是非常重要的工具,用于匹配、查找和替换文本中的特定字符串。然而,编写高效的正则表达式并不是一件易事,需要开发者具备一定的技巧和经验。下面是一些可以帮助您编写高效正则表达式的技巧:1.尽可能使用非贪婪匹配默认情况下,正则表达式是贪婪的,即它们将尽可能匹配更多的文本。在某些情况下,可能需要使用非贪婪匹配来避免这种情况。非贪婪匹配使用"

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version