如何从字符串中删除单词后的所有文本?

时间:2016-09-17 15:44:25

标签: php replace

我怎样才能从段落中删除一个单词?

例如:

如何从字符串中删除此(测试)单词后的所有文本?

ABC DEF GHEE JUL

我想从我的字符串中删除测试MON ...

并且刚刚:

{{1}}

由于

4 个答案:

答案 0 :(得分:1)

你可以轻松拆分

$string = "ABC DEF GHEE JUL test MON";
$splitted = explode("test", $string);
$result = $splitted[0]; //ABC DEF GHEE JUL

答案 1 :(得分:1)

最简单的事情就是简单地子串:

$result = substr($string, 0, strpos($string, 'test'));

答案 2 :(得分:-1)

您可以创建函数并在以后重复使用它们,例如:

$test1=removeAfterFirst("ABC DEF GHEE JUL test MON ...","test ");
// $test1 is "ABC DEF GHEE JUL "

$test2=removeAfterLast("ABC DEF ABC GHE","ABC");
// $test2 is "ABC DEF "

功能:

public static function removeAfterFirst($text,$value)
{
    $firstOccurence=strpos($text,$value);
    if($firstOccurence===false)
        return $text;
    return substr($text,0,$firstOccurence);
}

public static function removeAfterLast($text,$value)
{
    $lastOccurence=strrpos($text,$value);
    if($lastOccurence===false)
        return $text;
    return substr($text,0,$lastOccurence);
}

答案 3 :(得分:-3)

您可以尝试这样的事情:

@FXML
private AnchorPane rootSetPane;