0

我有以下字符串:

Johnny arrived at BOB
Peter is at SUSAN

我想要一个可以做到这一点的功能:

$string = stripWithWildCard("Johnny arrived at BOB", "*at ")

$string 必须等于 BOB。另外,如果我这样做:

$string = stripWithWildCard("Peter is at SUSAN", "*at ");

$string 必须等于 SUSAN。

最短的方法是什么?

4

1 回答 1

5

一个正则表达式。您替换并替换为空字符串.**

echo preg_replace('/.*at /', '', 'Johnny arrived at BOB');

请记住,如果字符串"*at "不是硬编码的,那么您还需要引用在正则表达式中具有特殊含义的任何字符。所以你会有:

$find = '*at ';
$find = preg_quote($find, '/');  // "/" is the delimiter used below
$find = str_replace('\*', '.*'); // preg_quote escaped that, unescape and convert

echo preg_replace('/'.$find.'/', '', $input);
于 2013-10-24T11:28:21.563 回答