찾다
백엔드 개발PHP 튜토리얼PHP常用的基本文件和目录操作总结,_PHP教程
PHP常用的基本文件和目录操作总结,_PHP教程Jul 13, 2016 am 10:18 AM
php그리고기초적인요약작동하다문서용적목차

PHP常用的基本文件和目录操作总结,

来源:http://www.ido321.com/835.html

一、基本文件的操作

文件的基本操作有:文件判断、目录判断、文件大小、读写性判断、存在性判断及文件时间等

<span id="lnum1">   1:</span><span> <?php</span>
<span id="lnum2">   2:</span><span>     header(<span>"content-type"</span>,<span>"text/html;charset=utf-8"</span>);</span>
<span id="lnum3">   3:</span><span> <span>/*</span></span>
<span id="lnum4">   4:</span><span> <span>*声明一个函数,传入文件名获取文件属性</span></span>
<span id="lnum5">   5:</span><span> <span>*@param string $fileName 文件名称</span></span>
<span id="lnum6">   6:</span><span> <span>*/</span></span>
<span id="lnum7">   7:</span><span> <span>function</span> getFilePro($fileName)</span>
<span id="lnum8">   8:</span><span> {</span>
<span id="lnum9">   9:</span><span>     <span>if</span>(!file_exists($fileName))</span>
<span id="lnum10">  10:</span><span>     {</span>
<span id="lnum11">  11:</span><span>         <span>echo</span> <span>'文件不存在<br/>'</span>;</span>
<span id="lnum12">  12:</span><span>         <span>return</span>;</span>
<span id="lnum13">  13:</span><span>     }</span>
<span id="lnum14">  14:</span><span>     <span>/*是否是普通文件*/</span></span>
<span id="lnum15">  15:</span><span>     <span>if</span>(is_file($fileName))</span>
<span id="lnum16">  16:</span><span>     {</span>
<span id="lnum17">  17:</span><span>         <span>echo</span> $fileName.<span>'是一个文件<br/>'</span>;</span>
<span id="lnum18">  18:</span><span>     }</span>
<span id="lnum19">  19:</span><span>     <span>/*是否是目录*/</span></span>
<span id="lnum20">  20:</span><span>     <span>if</span>(is_dir($fileName))</span>
<span id="lnum21">  21:</span><span>     {</span>
<span id="lnum22">  22:</span><span>         <span>echo</span> $fileName.<span>'是一个目录'</span>;</span>
<span id="lnum23">  23:</span><span>     }</span>
<span id="lnum24">  24:</span><span>     <span>/*输出文件的型态*/</span></span>
<span id="lnum25">  25:</span><span>     <span>echo</span> <span>'文件型态是:'</span>.getFileType($fileName).<span>'<br/>'</span>;</span>
<span id="lnum26">  26:</span><span>     <span>/*文件大小,转换单位*/</span></span>
<span id="lnum27">  27:</span><span>     <span>echo</span> <span>'文件大小是:'</span>.getFileSize(filesize($fileName)).<span>'<br/>'</span>;</span>
<span id="lnum28">  28:</span><span>     <span>/*文件是否可读*/</span></span>
<span id="lnum29">  29:</span><span>     <span>if</span>(is_readable($fileName))</span>
<span id="lnum30">  30:</span><span>     {</span>
<span id="lnum31">  31:</span><span>         <span>echo</span> <span>'文件可读<br/>'</span>;</span>
<span id="lnum32">  32:</span><span>     }</span>
<span id="lnum33">  33:</span><span>     <span>/*文件是否可写*/</span></span>
<span id="lnum34">  34:</span><span>     <span>if</span>(is_writable($fileName))</span>
<span id="lnum35">  35:</span><span>     {</span>
<span id="lnum36">  36:</span><span>         <span>echo</span> <span>'文件可写<br/>'</span>;</span>
<span id="lnum37">  37:</span><span>     }</span>
<span id="lnum38">  38:</span><span>     <span>/*文件是否可执行*/</span></span>
<span id="lnum39">  39:</span><span>     <span>if</span>(is_executable($fileName))</span>
<span id="lnum40">  40:</span><span>     {</span>
<span id="lnum41">  41:</span><span>         <span>echo</span> <span>'文件可执行<br/>'</span>;</span>
<span id="lnum42">  42:</span><span>     }</span>
<span id="lnum43">  43:</span>
<span id="lnum44">  44:</span><span>     <span>echo</span> <span>'文件创立时间:'</span>.date(<span>'Y年m月j日'</span>,filectime($fileName)).<span>'<br/>'</span>;</span>
<span id="lnum45">  45:</span><span>     <span>echo</span> <span>'文件最后修改时间:'</span>.date(<span>'Y年m月j日'</span>,filemtime($fileName)).<span>'<br/>'</span>;</span>
<span id="lnum46">  46:</span><span>     <span>echo</span> <span>'文件最后打开时间:'</span>.date(<span>'Y年m月j日'</span>,fileatime($fileName)).<span>'<br/>'</span>;</span>
<span id="lnum47">  47:</span><span> }</span>
<span id="lnum48">  48:</span>
<span id="lnum49">  49:</span><span> <span>/*</span></span>
<span id="lnum50">  50:</span><span> <span>*声明一个函数,返回文件类型</span></span>
<span id="lnum51">  51:</span><span> <span>*@param string $fileName 文件名称</span></span>
<span id="lnum52">  52:</span><span> <span>*/</span></span>
<span id="lnum53">  53:</span><span> <span>function</span> getFileType($fileName)</span>
<span id="lnum54">  54:</span><span> {</span>
<span id="lnum55">  55:</span><span>     $type = <span>''</span>;</span>
<span id="lnum56">  56:</span><span>     <span>switch</span>(filetype($fileName))</span>
<span id="lnum57">  57:</span><span>     {</span>
<span id="lnum58">  58:</span><span>         <span>case</span> <span>'file'</span>:$type .= <span>'普通文件'</span>;<span>break</span>;</span>
<span id="lnum59">  59:</span><span>         <span>case</span> <span>'dir'</span>:$type .= <span>'目录文件'</span>;<span>break</span>;</span>
<span id="lnum60">  60:</span><span>         <span>case</span> <span>'block'</span>:$type .= <span>'块设备文件'</span>;<span>break</span>;</span>
<span id="lnum61">  61:</span><span>         <span>case</span> <span>'char'</span>:$type .= <span>'字符设备文件'</span>;<span>break</span>;</span>
<span id="lnum62">  62:</span><span>         <span>case</span> <span>'filo'</span>:$type .= <span>'管道文件'</span>;<span>break</span>;</span>
<span id="lnum63">  63:</span><span>         <span>case</span> <span>'link'</span>:$type .= <span>'符号链接'</span>;<span>break</span>;</span>
<span id="lnum64">  64:</span><span>         <span>case</span> <span>'unknown'</span>:$type .= <span>'未知文件'</span>;<span>break</span>;</span>
<span id="lnum65">  65:</span><span>         <span>default</span>:</span>
<span id="lnum66">  66:</span><span>     }</span>
<span id="lnum67">  67:</span><span>     <span>return</span> $type;</span>
<span id="lnum68">  68:</span><span> }</span>
<span id="lnum69">  69:</span>
<span id="lnum70">  70:</span><span> <span>/*</span></span>
<span id="lnum71">  71:</span><span> <span>*声明一个函数,返回文件大小</span></span>
<span id="lnum72">  72:</span><span> <span>*@param int $bytes 文件字节数</span></span>
<span id="lnum73">  73:</span><span> <span>*/</span></span>
<span id="lnum74">  74:</span><span> <span>function</span> getFileSize($bytes)</span>
<span id="lnum75">  75:</span><span> {</span>
<span id="lnum76">  76:</span><span>     <span>if</span>($bytes >= pow(2,40))</span>
<span id="lnum77">  77:</span><span>     {</span>
<span id="lnum78">  78:</span><span>         $<span>return</span> = round($bytes / pow(1024,4),2);</span>
<span id="lnum79">  79:</span><span>         $suffix = <span>'TB'</span>;</span>
<span id="lnum80">  80:</span><span>     }</span>
<span id="lnum81">  81:</span><span>     <span>else</span> <span>if</span>($bytes >= pow(2,30))</span>
<span id="lnum82">  82:</span><span>     {</span>
<span id="lnum83">  83:</span><span>         $<span>return</span> = round($bytes / pow(1024,3),2);</span>
<span id="lnum84">  84:</span><span>         $suffix = <span>'GB'</span>;</span>
<span id="lnum85">  85:</span><span>     }</span>
<span id="lnum86">  86:</span><span>     <span>else</span> <span>if</span>($bytes >= pow(2,20))</span>
<span id="lnum87">  87:</span><span>     {</span>
<span id="lnum88">  88:</span><span>         $<span>return</span> = round($bytes / pow(1024,2),2);</span>
<span id="lnum89">  89:</span><span>         $suffix = <span>'MB'</span>;</span>
<span id="lnum90">  90:</span><span>     }</span>
<span id="lnum91">  91:</span><span>     <span>else</span> <span>if</span>($bytes >= pow(2,10))</span>
<span id="lnum92">  92:</span><span>     {</span>
<span id="lnum93">  93:</span><span>         $<span>return</span> = round($bytes / pow(1024,1),2);</span>
<span id="lnum94">  94:</span><span>         $suffix = <span>'KB'</span>;</span>
<span id="lnum95">  95:</span><span>     }</span>
<span id="lnum96">  96:</span><span>     <span>else</span></span>
<span id="lnum97">  97:</span><span>     {</span>
<span id="lnum98">  98:</span><span>         $<span>return</span> = $bytes;</span>
<span id="lnum99">  99:</span><span>         $suffix = <span>'B'</span>;</span>
<span id="lnum100"> 100:</span><span>     }</span>
<span id="lnum101"> 101:</span><span>     <span>return</span> $<span>return</span>.<span>" "</span>.$suffix;</span>
<span id="lnum102"> 102:</span><span> }</span>
<span id="lnum103"> 103:</span>
<span id="lnum104"> 104:</span><span> <span>/*调用函数,传入test目录下的test.txt文件*/</span></span>
<span id="lnum105"> 105:</span><span> getFilePro(<span>'./test/div.html'</span>);</span>
<span id="lnum106"> 106:</span><span> ?></span>
结果:


二、目录的操作

目录的操作有:遍历目录、删除、复制、大小统计等

1、遍历目录

<span id="lnum1">   1:</span><span> <span>/*</span></span>
<span id="lnum2">   2:</span><span> <span>*遍历目录</span></span>
<span id="lnum3">   3:</span><span> <span>*@param string $dirName 目录名</span></span>
<span id="lnum4">   4:</span><span> <span>*/</span></span>
<span id="lnum5">   5:</span><span> <span>function</span> findDir($dirName)</span>
<span id="lnum6">   6:</span><span> {</span>
<span id="lnum7">   7:</span><span>     $num  = 0;  <span>/*统计子文件个数*/</span></span>
<span id="lnum8">   8:</span><span>     $dir_handle = opendir($dirName);  <span>/*打开目录*/</span></span>
<span id="lnum9">   9:</span><span>     <span>/*输出目录文件*/</span></span>
<span id="lnum10">  10:</span><span>     <span>echo</span> <span>'<table border="0" align="center" width="600" cellspacing="0" cellpadding="0">'</span>;</span>
<span id="lnum11">  11:</span><span>     <span>echo</span> <span>'<caption><h2 id="目录-span-dirName-span-下的文件">目录'</span>.$dirName.<span>'下的文件</h2></caption>'</span>;</span>
<span id="lnum12">  12:</span><span>     <span>echo</span> <span>'<tr align="left" bgcolor="#cccccc"'</span>;</span>
<span id="lnum13">  13:</span><span>     <span>echo</span> <span>'<th>文件名</th><th>文件大小</th><th>文件类型</th><th>修改时间</th></tr>'</span>;</span>
<span id="lnum14">  14:</span>
<span id="lnum15">  15:</span><span>     <span>while</span>($file = readdir($dir_handle))</span>
<span id="lnum16">  16:</span><span>     {</span>
<span id="lnum17">  17:</span><span>         $dirFile = $dirName.<span>'/'</span>.$file;</span>
<span id="lnum18">  18:</span><span>         $bgcolor = $num++%2==0?<span>'#ffffff'</span>:<span>'#cccccc'</span>;</span>
<span id="lnum19">  19:</span><span>         <span>echo</span> <span>'<tr bgcolor='</span>.$bgcolor.<span>'>'</span>;</span>
<span id="lnum20">  20:</span><span>         <span>echo</span> <span>'<td>'</span>.$file.<span>'</td>'</span>;</span>
<span id="lnum21">  21:</span><span>         <span>echo</span> <span>'<td>'</span>.filesize($dirFile).<span>'</td>'</span>;</span>
<span id="lnum22">  22:</span><span>         <span>echo</span> <span>'<td>'</span>.filetype($dirFile).<span>'</td>'</span>;</span>
<span id="lnum23">  23:</span><span>         <span>echo</span> <span>'<td>'</span>.date(<span>'Y/n/t'</span>,filemtime($dirFile)).<span>'</td>'</span>;</span>
<span id="lnum24">  24:</span><span>         <span>echo</span> <span>'</tr>'</span>;</span>
<span id="lnum25">  25:</span><span>     }</span>
<span id="lnum26">  26:</span><span>     <span>echo</span> <span>"</table>"</span>;</span>
<span id="lnum27">  27:</span><span>     closedir($dir_handle);  <span>/*关闭目录*/</span></span>
<span id="lnum28">  28:</span><span>     <span>echo</span> <span>"在<b>"</span>.$dirName.<span>"</b>目录下共有<b>"</span>.$num.<span>'</b>个子文件'</span>;</span>
<span id="lnum29">  29:</span><span> }</span>
<span id="lnum30">  30:</span><span> <span>/*传递当前目录下的test目录*/</span></span>
<span id="lnum31">  31:</span><span> findDir(<span>'./test'</span>);</span>

结果



2、统计目录大小

<span id="lnum1">   1:</span><span> <span>/*</span></span>
<span id="lnum2">   2:</span><span> <span>*统计目录大小</span></span>
<span id="lnum3">   3:</span><span> <span>*@param string $dirName 目录名</span></span>
<span id="lnum4">   4:</span><span> <span>*@return double</span></span>
<span id="lnum5">   5:</span><span> <span>*/</span></span>
<span id="lnum6">   6:</span>
<span id="lnum7">   7:</span><span> <span>function</span> dirSize($dirName)</span>
<span id="lnum8">   8:</span><span> {</span>
<span id="lnum9">   9:</span><span>     $dir_size = 0;</span>
<span id="lnum10">  10:</span><span>     <span>if</span>($dir_handle = @opendir($dirName))</span>
<span id="lnum11">  11:</span><span>     {</span>
<span id="lnum12">  12:</span><span>         <span>while</span> ($fileName = readdir($dir_handle))</span>
<span id="lnum13">  13:</span><span>         {</span>
<span id="lnum14">  14:</span>              /*排除两个特殊目录*/
<span id="lnum15">  15:</span><span>             <span>if</span>($fileName != <span>'.'</span> && $fileName != <span>'..'</span>)</span>
<span id="lnum16">  16:</span><span>             {</span>
<span id="lnum17">  17:</span><span>                 $subFile = $dirName.<span>'/'</span>.$fileName;</span>
<span id="lnum18">  18:</span><span>                 <span>if</span>(is_file($subFile))</span>
<span id="lnum19">  19:</span><span>                 {</span>
<span id="lnum20">  20:</span><span>                     $dir_size += filesize($subFile);</span>
<span id="lnum21">  21:</span><span>                 }</span>
<span id="lnum22">  22:</span><span>                 <span>if</span>(is_dir($subFile))</span>
<span id="lnum23">  23:</span><span>                 {</span>
<span id="lnum24">  24:</span><span>                     $dir_size += dirSize($subFile);</span>
<span id="lnum25">  25:</span><span>                 }</span>
<span id="lnum26">  26:</span><span>             }</span>
<span id="lnum27">  27:</span><span>         }</span>
<span id="lnum28">  28:</span><span>         closedir($dir_handle);</span>
<span id="lnum29">  29:</span><span>         <span>return</span> $dir_size;</span>
<span id="lnum30">  30:</span><span>     }</span>
<span id="lnum31">  31:</span><span> }</span>
<span id="lnum32">  32:</span><span> <span>/*传递当前目录下的test目录*/</span></span>
<span id="lnum33">  33:</span><span> $dir_size = dirSize(<span>'./test'</span>);</span>
<span id="lnum34">  34:</span><span> <span>echo</span> <span>'./test目录文件大小是:'</span>.round($dir_size / pow(1024,1),2).<span>'KB'</span>;</span>

结果



3、删除目录

<span id="lnum1">   1:</span><span> <span>/*</span></span>
<span id="lnum2">   2:</span><span> <span>*删除目录</span></span>
<span id="lnum3">   3:</span><span> <span>*@param string $dirName 目录名</span></span>
<span id="lnum4">   4:</span><span> <span>*/</span></span>
<span id="lnum5">   5:</span><span> <span>function</span> delDir($dirName)</span>
<span id="lnum6">   6:</span><span> {</span>
<span id="lnum7">   7:</span><span>     <span>/*php中的mkdir函数就可以创建目录*/</span></span>
<span id="lnum8">   8:</span><span>     <span>if</span>(file_exists($dirName))</span>
<span id="lnum9">   9:</span><span>     {</span>
<span id="lnum10">  10:</span><span>         <span>if</span>($dir_handle = @opendir($dirName))</span>
<span id="lnum11">  11:</span><span>         {</span>
<span id="lnum12">  12:</span><span>             <span>while</span> ($fileName = readdir($dir_handle))</span>
<span id="lnum13">  13:</span><span>             {</span>
<span id="lnum14">  14:</span>                  /*排除两个特殊目录*/
<span id="lnum15">  15:</span><span>                 <span>if</span>($fileName != <span>'.'</span> && $fileName != <span>'..'</span>)</span>
<span id="lnum16">  16:</span><span>                 {</span>
<span id="lnum17">  17:</span><span>                     $subFile = $dirName.<span>'/'</span>.$fileName;</span>
<span id="lnum18">  18:</span><span>                     <span>if</span>(is_file($subFile))</span>
<span id="lnum19">  19:</span><span>                     {</span>
<span id="lnum20">  20:</span><span>                         unlink($subFile);</span>
<span id="lnum21">  21:</span><span>                     }</span>
<span id="lnum22">  22:</span><span>                     <span>if</span>(is_dir($subFile))</span>
<span id="lnum23">  23:</span><span>                     {</span>
<span id="lnum24">  24:</span><span>                         delDir($subFile);</span>
<span id="lnum25">  25:</span><span>                     }</span>
<span id="lnum26">  26:</span><span>                 }</span>
<span id="lnum27">  27:</span><span>             }</span>
<span id="lnum28">  28:</span><span>             closedir($dir_handle);</span>
<span id="lnum29">  29:</span><span>             rmdir($dirName);</span>
<span id="lnum30">  30:</span><span>             <span>return</span> $dirName.<span>'目录已经删除'</span>;</span>
<span id="lnum31">  31:</span><span>         }</span>
<span id="lnum32">  32:</span><span>     }</span>
<span id="lnum33">  33:</span><span> }</span>
<span id="lnum34">  34:</span><span> <span>/*传递test目录的副本test1*/</span></span>
<span id="lnum35">  35:</span><span> <span>echo</span> delDir(<span>'./test1'</span>);</span>

删除成功的提示信息



4、复制目录

<span id="lnum1">   1:</span><span> <span>/*</span></span>
<span id="lnum2">   2:</span><span> <span>*复制目录</span></span>
<span id="lnum3">   3:</span><span> <span>*@param string $dirSrc 原目录名</span></span>
<span id="lnum4">   4:</span><span> <span>*@param string $dirTo 目标目录名</span></span>
<span id="lnum5">   5:</span><span> <span>*/</span></span>
<span id="lnum6">   6:</span> function copyDir($dirSrc,$dirTo)
<span id="lnum7">   7:</span><span> {</span>
<span id="lnum8">   8:</span><span>     <span>if</span>(is_file($dirTo))</span>
<span id="lnum9">   9:</span><span>     {</span>
<span id="lnum10">  10:</span><span>         <span>echo</span> <span>'目标目录不能创建'</span>;<span>/*目标不是一个*/</span></span>
<span id="lnum11">  11:</span><span>         <span>return</span>;</span>
<span id="lnum12">  12:</span><span>     }</span>
<span id="lnum13">  13:</span><span>     <span>if</span>(!file_exists($dirTo))</span>
<span id="lnum14">  14:</span>     {
<span id="lnum15">  15:</span><span>         <span>/*目录不存在则创建*/</span></span>
<span id="lnum16">  16:</span><span>         mkdir($dirTo);</span>
<span id="lnum17">  17:</span><span>     }</span>
<span id="lnum18">  18:</span><span>     <span>if</span>($dir_handle = @opendir($dirSrc))</span>
<span id="lnum19">  19:</span><span>     {</span>
<span id="lnum20">  20:</span><span>         <span>while</span> ($fileName = readdir($dir_handle))</span>
<span id="lnum21">  21:</span><span>         {</span>
<span id="lnum22">  22:</span><span>              <span>/*排除两个特殊目录*/</span></span>
<span id="lnum23">  23:</span><span>             <span>if</span>($fileName != <span>'.'</span> && $fileName != <span>'..'</span>)</span>
<span id="lnum24">  24:</span><span>             {</span>
<span id="lnum25">  25:</span><span>                 $subSrcFile = $dirSrc.<span>'/'</span>.$fileName;</span>
<span id="lnum26">  26:</span><span>                 $subToFile = $dirTo.<span>'/'</span>.$fileName;</span>
<span id="lnum27">  27:</span><span>                 <span>if</span>(is_file($subSrcFile))</span>
<span id="lnum28">  28:</span><span>                 {</span>
<span id="lnum29">  29:</span><span>                     copy($subSrcFile,$subToFile);</span>
<span id="lnum30">  30:</span><span>                 }</span>
<span id="lnum31">  31:</span><span>                 <span>if</span>(is_dir($subSrcFile))</span>
<span id="lnum32">  32:</span><span>                 {</span>
<span id="lnum33">  33:</span><span>                     copyDir($subSrcFile,$subToFile);</span>
<span id="lnum34">  34:</span><span>                 }</span>
<span id="lnum35">  35:</span><span>             }</span>
<span id="lnum36">  36:</span><span>         }</span>
<span id="lnum37">  37:</span><span>         closedir($dir_handle);</span>
<span id="lnum38">  38:</span><span>         <span>return</span> $dirSrc.<span>'目录已经复制到'</span>.$dirTo.<span>'目录'</span>;</span>
<span id="lnum39">  39:</span><span>     }</span>
<span id="lnum40">  40:</span><span> }</span>
<span id="lnum41">  41:</span><span> <span>echo</span> copyDir(<span>'./test'</span>,<span>'../testcopy'</span>);</span>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/885998.htmlTechArticlePHP常用的基本文件和目录操作总结, 来源:http://www.ido321.com/835.html 一、基本文件的操作 文件的基本操作有:文件判断、目录判断、文件大...
성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

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

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

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

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

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

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

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

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

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

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)”语句。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

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

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기

VSCode Windows 64비트 다운로드

VSCode Windows 64비트 다운로드

Microsoft에서 출시한 강력한 무료 IDE 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.