Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中使用正则表达式删除指定字符串之后的特定字符串?_Php_Regex_String - Fatal编程技术网

如何在php中使用正则表达式删除指定字符串之后的特定字符串?

如何在php中使用正则表达式删除指定字符串之后的特定字符串?,php,regex,string,Php,Regex,String,我有一根像下面这样的线 [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] =&

我有一根像下面这样的线

 [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

我怎样才能做到这一点呢?

如果所有字符串都是这种格式,您可以使用基本的字符串函数

$str = "[Session #1: Tues May 31st - Fri June 10th (1-5:30PM)#1 only: 1pmADV]";
$str = substr($str, 0, strpos($str, "pm") + 2);
$str .= "]";
$str=“[Session#1:5月31日星期二至6月10日星期五(下午1-5:30)#1仅限1:1pmADV]=>2”
echo preg_替换('/(?如何:

$str = preg_replace("/(pm)[^\]]+(\])/", "$1$2", $str);
这将删除在
pm
和第一次出现的
]
之间不是
]
的所有字符

$str = preg_replace("/(pm)[^\]]+(\])/", "$1$2", $str);