0

让我们从一个例子开始。在文件 file.php 中说存在以下文本。

This is just to show example. This is just to show example. This is just to show example. This is just to show example. end.  Okay This is just to show example. This is just to show example.

现在在上面的文本中,如果我发现没问题,那么我将删除包括“好”一词在内的其余内容并将文件保存在服务器中。

有人请告诉我一个有效的方法来做到这一点。

4

4 回答 4

0
$pos=strpos ($inputstring, 'okay');//test to see if there is a match (it will find the first match)
if($pos!==false){//if there was a match
   $cutstring=substr($inputstring, 0, $pos);//extract a substring from the begining to the match
   //echo $cutstring
}

在此处阅读有关这些功能的更多信息:

http://php.net/manual/en/function.substr.php

http://php.net/manual/en/function.strpos.php

于 2012-10-25T18:30:20.450 回答
0

有很多不同的方法可以做到这一点strposregex。这是一个例子

$str = 'This is just to show example. This is just to show example. This is just to show example. This is just to show example. end.  Okay This is just to show example. This is just to show example.';

preg_match('%Okay%', $str, $match);

if($match){
  // save file to server 
}
于 2012-10-25T18:32:50.980 回答
0
$str = "This is just to show example. This is just to show example. This is just to show  example. This is just to show example. end.  Okay This is just to show example. This is just to show example.";
$fnd = "Okay";
if (false !== $p = stripos($str, $fnd)) $str = substr($str, 0, $p);
echo $str; // Now save it or whatever.
于 2012-10-25T18:37:56.660 回答
0

您还可以使用explode拆分内容。根据您的问题的广泛程度,这可能不是最好的解决方案,但我想我也会包括它:

$string = $your_file_content
$split = explode('okay', strtolower($string));
//$split[0] = everything before 'okay';
//$split[1] = everything after 'okay';
于 2012-10-25T18:38:07.953 回答