我有一个如下所示的字符串,
[Session #1: Tues May 31st - Fri June 10th (1-5:30PM)#1 only: 1pmADV] => 2
[Session #1: Tues May 31st - Fri June 10th (1-5:30PM)#1 only: 2pmADV] => 1
[Session #2: Tues June 14th - Fri June 24th (9-2:00PM)#1 only: 2pmADV] => 1
[Session #1: Tues May 31st - Fri June 10th (1-5:30PM)#1 only: 1pmN/S+] => 1
[Session #2: Tues June 14th - Fri June 24th (9-2:00PM)#1 only: 2pmN/S+] => 1
[Session #2: Tues June 14th - Fri June 24th (9-2:00PM)#1 only: 3:30pmN/S+] => 1
我需要删除字符串“pm”之后的文本,直到首次出现“]”。
例如,
from
[Session #1: Tues May 31st - Fri June 10th (1-5:30PM)#1 only: 1pmADV] => 2
to
[Session #1: Tues May 31st - Fri June 10th (1-5:30PM)#1 only: 1pm] => 2
我该怎么做?
答案 0 :(得分:1)
如果所有字符串都采用这种格式,则可以使用基本字符串函数
$str = "[Session #1: Tues May 31st - Fri June 10th (1-5:30PM)#1 only: 1pmADV]";
$str = substr($str, 0, strpos($str, "pm") + 2);
$str .= "]";
答案 1 :(得分:0)
$str = "[Session #1: Tues May 31st - Fri June 10th (1-5:30PM)#1 only: 1pmADV] => 2"
echo preg_replace('/(?<=pm)\S*?(?=])/is', '', $str);
答案 2 :(得分:0)
怎么样:
$str = preg_replace("/(pm)[^\]]+(\])/", "$1$2", $str);
这将删除]
与pm
第一次出现之间]
之内的所有字符