php 读写选项卡分隔文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 读写选项卡分隔文件相关的知识,希望对你有一定的参考价值。

<span style="color: #808080; font-style: italic;">//</span>
<span style="color: #808080; font-style: italic;">// save an array as tab seperated text file</span>
<span style="color: #808080; font-style: italic;">//</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> write_tabbed_file<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filepath</span>, <span style="color: #0000ff;">$array</span>, <span style="color: #0000ff;">$save_keys</span>=<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$content</span> = <span style="color: #ff0000;">''</span>;
&nbsp;
    <a href="http://www.php.net/reset"><span style="color: #000066;">reset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$array</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/list"><span style="color: #000066;">list</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$key</span>, <span style="color: #0000ff;">$val</span><span style="color: #66cc66;">&#41;</span> = <a href="http://www.php.net/each"><span style="color: #000066;">each</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$array</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// replace tabs in keys and values to [space]</span>
        <span style="color: #0000ff;">$key</span> = <a href="http://www.php.net/str_replace"><span style="color: #000066;">str_replace</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>, <span style="color: #ff0000;">&quot; &quot;</span>, <span style="color: #0000ff;">$key</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$val</span> = <a href="http://www.php.net/str_replace"><span style="color: #000066;">str_replace</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>, <span style="color: #ff0000;">&quot; &quot;</span>, <span style="color: #0000ff;">$val</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$save_keys</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #0000ff;">$content</span> .=  <span style="color: #0000ff;">$key</span>.<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>; <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// create line:</span>
        <span style="color: #0000ff;">$content</span> .= <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/is_array"><span style="color: #000066;">is_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$val</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> ? <a href="http://www.php.net/implode"><span style="color: #000066;">implode</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>, <span style="color: #0000ff;">$val</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0000ff;">$val</span>;
        <span style="color: #0000ff;">$content</span> .= <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/file_exists"><span style="color: #000066;">file_exists</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filepath</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; !<a href="http://www.php.net/is_writeable"><span style="color: #000066;">is_writeable</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filepath</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> 
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fp</span> = <a href="http://www.php.net/fopen"><span style="color: #000066;">fopen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filepath</span>, <span style="color: #ff0000;">'w+'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <a href="http://www.php.net/fwrite"><span style="color: #000066;">fwrite</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fp</span>, <span style="color: #0000ff;">$content</span><span style="color: #66cc66;">&#41;</span>;
        <a href="http://www.php.net/fclose"><span style="color: #000066;">fclose</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fp</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>; <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//</span>
<span style="color: #808080; font-style: italic;">// load a tab seperated text file as array</span>
<span style="color: #808080; font-style: italic;">//</span>
<span style="color: #000000; font-weight: bold;">function</span> load_tabbed_file<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filepath</span>, <span style="color: #0000ff;">$load_keys</span>=<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$array</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<a href="http://www.php.net/file_exists"><span style="color: #000066;">file_exists</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filepath</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$array</span>; <span style="color: #66cc66;">&#125;</span>
    <span style="color: #0000ff;">$content</span> = <a href="http://www.php.net/file"><span style="color: #000066;">file</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filepath</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$x</span>=<span style="color: #cc66cc;">0</span>; <span style="color: #0000ff;">$x</span> &lt; <a href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$content</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #0000ff;">$x</span>++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/trim"><span style="color: #000066;">trim</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$content</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$x</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> != <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$line</span> = <a href="http://www.php.net/explode"><span style="color: #000066;">explode</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>, <a href="http://www.php.net/trim"><span style="color: #000066;">trim</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$content</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$x</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$load_keys</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
                <span style="color: #0000ff;">$key</span> = <a href="http://www.php.net/array_shift"><span style="color: #000066;">array_shift</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$line</span><span style="color: #66cc66;">&#41;</span>;
                <span style="color: #0000ff;">$array</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$key</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$line</span>;
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #0000ff;">$array</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$line</span>; <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$array</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*
** Example usage:
*/</span>
&nbsp;
<span style="color: #0000ff;">$array</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
    <span style="color: #ff0000;">'line1'</span>  =&gt; <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'data-1-1'</span>, <span style="color: #ff0000;">'data-1-2'</span>, <span style="color: #ff0000;">'data-1-3'</span><span style="color: #66cc66;">&#41;</span>,
    <span style="color: #ff0000;">'line2'</span> =&gt; <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'data-2-1'</span>, <span style="color: #ff0000;">'data-2-2'</span>, <span style="color: #ff0000;">'data-2-3'</span><span style="color: #66cc66;">&#41;</span>,
    <span style="color: #ff0000;">'line3'</span>  =&gt; <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'data-3-1'</span>, <span style="color: #ff0000;">'data-3-2'</span>, <span style="color: #ff0000;">'data-3-3'</span><span style="color: #66cc66;">&#41;</span>,
    <span style="color: #ff0000;">'line4'</span> =&gt; <span style="color: #ff0000;">'foobar'</span>,
    <span style="color: #ff0000;">'line5'</span> =&gt; <span style="color: #ff0000;">'hello world'</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// save the array to the data.txt file:</span>
write_tabbed_file<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'data.txt'</span>, <span style="color: #0000ff;">$array</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/* the data.txt content looks like this:
line1	data-1-1	data-1-2	data-1-3
line2	data-2-1	data-2-2	data-2-3
line3	data-3-1	data-3-2	data-3-3
line4	foobar
line5	hello world
*/</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// load the saved array:</span>
<span style="color: #0000ff;">$reloaded_array</span> = load_tabbed_file<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'data.txt'</span>,<span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<a href="http://www.php.net/print_r"><span style="color: #000066;">print_r</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$reloaded_array</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// returns the array from above</span>

以上是关于php 读写选项卡分隔文件的主要内容,如果未能解决你的问题,请参考以下文章

FileHelpers 选项卡“\t”分隔阅读器

使用 split 使用分隔符选项卡“\t”在 Java 中解析字符串

使用 Bootstrap 4 在主活动选项卡错误中加载选项卡内容 - PHP

PHP PHP强制文件下载而不是在新选项卡/窗口中打开

有啥方法可以在CSV中输入用逗号分隔的数字?

PHP强制文件下载,而不是在新的选项卡/窗口中打开