繁体   English   中英

如何读取文本文件,直到PHP中的某些字符

[英]how to Read a text file until certain character in php

有什么方法可以选择文本文件的一部分,直到某个单词? 例如,我们有以下文本:

hello my name is Ehsan i'm from iran 
<hr>
I'm 20 years old and ...

我想从<hr>直到<hr>将它保存到另一个文件。

您应该使用PHP的explode函数分割字符串(不建议使用str_split)-它非常有用: http : //php.net/manual/zh/function.explode.php

  <?php
       //String we want to split
       $string = 'Hello<hr />World';

       //Splits the string into an array of strings delimited by <hr />
       $newString = explode('<hr />', $string);

       //So by that logic the first element at index 0 will be Hello
       //The second element will be at index 1 and will be World

       //So let's write the first element, up until <hr /> to the file
       file_put_contents('hello.txt', $newString[0]);

       //Note: $string still contains the original string
  ?>

因此explode()将参数1作为定界符,并将参数2作为字符串,并将其转换为在给定点分割的字符串数组。

请享用! :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM