【问题标题】:Php Splitting a txt file by full stopPhp通过句号拆分txt文件
【发布时间】:2014-03-30 22:56:17
【问题描述】:

在我的文本文件中,我有这样的句子:

词词词词词。 双字字字字字。 三字字字字字。 四字字字字字。 五字一字一字。

我想用句号分割文本文件并将每个句子放在一个新的文本文件中。我知道我必须使用 file_put_content 和 foreach 语句,但我正在努力解决它。 请帮忙!

【问题讨论】:

    标签: php file if-statement foreach explode


    【解决方案1】:

    您可以使用explode() 通过句号分割内容:

    $text = file_get_contents('file.txt');
    $sentences = explode('.', $text);
    
    // do stuff with sentences array
    

    【讨论】:

      【解决方案2】:
      $text = "word word word word word. twoword word word word word. threeword word word word word. fourword word word word word. fiveword word word word word.";
      
      $splitted_text = explode(".", $text);
      
      
      for($i = 0; $i < count($splitted_text); $i++){
          file_put_contents("file_$i", $splitted_text[$i].'.');
      
          //to display a file just use echo file_get_contents($file_name) as such :
          echo "file $i :<br/>";
          echo file_get_contents("file_$i"); //note that you could echo $splitted_text[$i]
          echo "<hr/>";
      }
      

      【讨论】:

      • implode加入一个数组,你的意思是explode吗?
      • 谢谢,我将如何回显文本文件的每一行,以及声明
      • 对不起什么?没看懂。
      • 表示如何在 php 中显示新创建的文本文件,如果这有意义的话:~
      猜你喜欢
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多