Есть ли способ выделить часть текстового файла до определенного слова?
например, у нас есть текст ниже:
hello my name is Ehsan i'm from iran
<hr>
I'm 20 years old and ...
Я хочу выбрать из привет, пока <hr>
сохранить его в другой файл.
Вы должны использовать PHP функцию разнесения, чтобы разбить строку (str_split устарела) — это довольно полезно:
http://php.net/manual/en/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 в качестве строки и преобразует его в массив строк, разбитых в данной точке.
Наслаждайтесь! 🙂
Других решений пока нет …