如何找到单词并删除它后面的单词?

How find words and strip the words after it?

$string = 'hello world php is a bla bla..';

如何通过查找字符串是否包含任何 'is' 来保留 'hello world php',然后删除它之后的所有字符?

我不能使用 substr 因为 'is' 的出现不一致,而且它可能是可重复的。如何抓住第一个?

这样试试,

$string = 'hello world php is a bla bla..';
    $str= substr($string, 0, strpos($string, "is"));

你也可以使用 preg_replace,

$str = preg_replace('/ is.*/', '', $string );

你可以使用爆炸:

<?php

$string = 'hello world php is a bla bla..';

$string = explode(' is ', $string);

echo $string[0];

?>

阅读更多内容:

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