有没有办法在某个单词之前选择文本文件的一部分? 例如,我们有以下文字:
hello my name is Ehsan i'm from iran
<hr>
I'm 20 years old and ...
我想从hello中选择,直到<hr>
将其保存到另一个文件。
答案 0 :(得分:0)
你应该使用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()将param 1作为分隔符,并将param 2作为字符串,并将其转换为在给定点分割的字符串数组。
享受! :)