的fwrite(); 在特定行之前和之后

的fwrite(); 在特定行之前和之后

问题描述:

I'm now using the function fwrite(); in PHP. But i want to locate my new things after a specific rule.

This wil be the output.

<?xml version="1.0" encoding="utf-8" ?> 
<logs>
    <log type="text">the new log</log>
    <log type="text>the old log</log>
    <log type="login">some other log.</log>
</logs>

How can i get the new log in the new log and not on the end. I only can find something like file_get_contents and then str_replace. But that seems really not efficient.

My php Code:

$file = $this->path.'logs.xml';
    // Open our file. And Create file if it doesn't exsist
    $fopen = fopen($file, "w+");

    // Looks if file is empty.
    if(filesize($file) == 0) {

        /*
         * Put your data in XML data.
         */
        $xmlData = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> 
";
        $xmlData .= "<logs> 
";
            $xmlData .= "\t<log type=\"".$data[0]."\">
";
                $xmlData .= "\t\t<author>".$data[1]."</author>
";
                $xmlData .= "\t\t<action>".$data[2]."</action>
";
                $xmlData .= "\t\t<result>".$data[3]."</result>
";
                $xmlData .= "\t\t<note>".$data[4]."</note>
";
            $xmlData .- "\t</log>
";
        $xmlData .= "</logs>";

    } else {



    }

    if(is_writeable($file)) {

        fwrite($fopen, $xmlData);
        return true;

    }
    return false;
    fclose($fopen);

Sincerely thank you.

我现在正在使用函数fwrite(); 用PHP。 但是我希望在特定规则之后找到我的新东西。 p>

这将是输出。 p>

 &lt;?xml version =  “1.0”encoding =“utf-8”?&gt;  
&lt; logs&gt; 
&lt; log type =“text”&gt;新日志&lt; / log&gt; 
&lt; log type =“text&gt;旧日志&lt; / log&gt; 
&lt; log type =”登录 “&gt;其他一些日志。&lt; / log&gt; 
&lt; / logs&gt; 
  code>  pre> 
 
 

如何在新日志中获取新日志,而不是在新日志中 我只能找到类似file_get_contents然后str_replace的内容。但这似乎效率不高。 p>

我的PHP代码: p>

  $  file = $ this-&gt; path.'logs.xml'; 
 //打开我们的文件。如果不存在则创建文件
 $ fopen = fopen($ file,“w +”); 
 \  n //查看文件是否为空。
 if(filesize($ file)== 0){
 
 / * 
 *将数据放入XML数据中。
 * / 
 $ xmlData =“&lt  ;?xml version = \“1.0 \”encoding = \“utf-8 \”?&gt;  
 
“; 
 $ xmlData。=”&lt; logs&gt;  
 
“; 
 $ xmlData。=”\ t&lt; log type = \“”。$ data [0]。“\”&gt; 
 
“; 
 $ xmlData。=”\ t \  t&lt; author&gt;“。$ data [1]。”&lt; / author&gt; 
 
“; 
 $ xmlData。=”\ t \ t&lt; action&gt;“。$ data [2]。”&lt; / 操作&gt; 
 
“; 
 $ xmlData。=”\ t \ t&lt; result&gt;“。$ data [3]。”&lt; / result&gt; 
 
“; 
 $ xmlData。=”\  t \ t&lt; note&gt;“。$ data [4]。”&lt; / note&gt; 
 
“; 
 $ xmlData .-”\ t&lt; / log&gt; 
 
“; 
 $ xmlData。  =“&lt; / logs&gt;”; 
 
}其他{
 
 
 
} 
 
 if(is_writeable($ file)){
 
 fwrite($ fopen,$ xmlData)  ; 
返回true; 
 
} 
返回false; 
 fclose($ fopen); 
  code>  pre> 
 
 

真诚地感谢你。 p> \ n div>

Well, you're lucky your data is in XML. PHP has got a bunch of easy to use libraries (extensions) that deal with XML data. For example SimpleXML or the more capable DOM (both extension are enabled by default).

<?php
    $filename = $this->path.'logs.xml';

    if (!file_exists($filename)) {
       // Here's your code from above, although it would be easier to use
       // the libraries here, as well
    } else {
       $logs = simplexml_load_file($filename);
       // See if there's a "text" log element
       $txtlog = $logs->xpath('./log[@type = "text"]');
       ...
    }

You could use the array_splice method. This way you can insert a new element in an array at any position.

$file = $this->path.'logs.xml';

$content = file($file); //is array with all lines as elements.

/*
0: <?xml version="1.0" encoding="utf-8" ?> 
1: <logs>
2:    <log type="text>the old log</log>
3:    <log type="login">some other log.</log>
4: </logs>
*/

//insert the new line at position 2
array_splice( $content, 2, 0, '    <log type="text">the new log</log>' );

/*
0: <?xml version="1.0" encoding="utf-8" ?> 
1: <logs>
2:    <log type="text">the new log</log>
3:    <log type="text>the old log</log>
4:    <log type="login">some other log.</log>
5: </logs>
*/

$fopen = fopen($file, "w+");
fwrite($fopen, implode("
", $content);
fclose($fopen);