假设我有以下文本:
This is line number 1.
This is my line number 2.
This is his line number 3.
This is her line number 4.
This is their line number 5.
我想删除单词 number
及其后面的所有内容并返回剩下的内容(当然,在单独的行上)。
我希望这是可能的,我没有使问题过于复杂,谢谢!
strstr() 是为你而生的,与 explode
一起$string = 'This is line number 1.
This is my line number 2.
This is his line number 3.
This is her line number 4.
This is their line number 5.';
$ex = explode('.',$string);
var_dump($ex);
foreach ($ex as $x) {
echo strstr($x, 'number',true)."<br>";
}
你可以使用 strpos 来查找单词 number
,然后对文本进行子串串(每行都在循环中)
http://php.net/manual/en/function.strpos.php。http://php.net/manual/en/function.substr.php在点之后扩展文本并在生成的数组上循环。希望它有帮助,我呵呵
所以这是工作示例:
$string = "This is line number 1";
$pos = strpos($string, "number");
if ($pos == false) {
print "Not found'n";
} else {
print "Found!'n";
print substr($string, $pos);
}
str_replace("number",",$actualstring)也可以正常工作。