从一个字符、该字符和一个空格的左侧剥离文本


Strip text from from the left of a character, and that character and a space, too

我有一个由get_the_title();(一个WordPress函数)生成的文本字符串,它在文本中间有一个冒号:

what I want to strip in the title : what I want to keep in the title

我需要去掉冒号左侧的文本,以及冒号本身和冒号右侧的单个空格。

我用这个来获取标题,并将文本剥离到冒号的左侧,

$mytitle = get_the_title();
$mytitle = strstr($mytitle,':'); 
echo $mytitle;

但我也试图用这个来去除冒号和它右边的空格

substr($mytitle(''), 2);

像这样:

$mytitle = get_the_title(); 
$mytitle = strstr($mytitle,':'); 
substr($mytitle(''), 2);
echo $mytitle;

但是我得到了一个php错误。

有没有办法把strstrsubstr结合起来?

或者有没有另一种方法——也许是用正则表达式(我不知道)——去掉冒号左边的所有内容,包括冒号和它右边的单个空格?

正则表达式将是完美的:

$mytitle = preg_replace(
    '/^    # Start of string
    [^:]*  # Any number of characters except colon
    :      # colon
    [ ]    # space/x', 
    '', $mytitle);

或者,作为一个线性:

$mytitle = preg_replace('/^[^:]*: /', '', $mytitle);

您可以这样做:

$mytitle = ltrim(explode(':', $mytitle, 2)[1]);

$title = preg_replace("/^.+:'s/", "", "This can be stripped: But this is needed");