使用preg_match或Symfony dom crawler获取php中两个char之间的字符串

问题描述 投票:1回答:1

我有这样的字符串

power.S04E10.you......

我想在字符串中获得0410

注意:在s char和e char之后它总是两位数。

在我的项目中,我已经使用了Symfony dom crawler

我很欣赏任何使用dom crawler或preg_match的解决方案

性能是一个问题,因为代码放在具有大量实例的循环中。

到目前为止,我编写了这段代码,它给了我上面字符串中的s04e10部分。我不知道如何分别获得0410

$matches = [];
$s = 'Power.S04E10.You.Cant.Fix.This.720p.&.1080p.NF.WEB-DL.DD5.1.x264-NTb';
$t = preg_match('/s([0-9]){2}e([0-9]){2}/i', $s, $matches);

提前致谢

string preg-match
1个回答
1
投票

你可以用

$s = 'Power.S04E10.You.Cant.Fix.This.720p.&.1080p.NF.WEB-DL.DD5.1.x264-NTb';
if (preg_match('/\.s([0-9]+)e([0-9]+)\./i', $s, $matches)) {
    echo $matches[1] . " - " . $matches[2]; // => 04 - 10
}

参见PHP demoonline regex demo

我假设你在点之间有SXXEYY,如果没有,用\.替换\b,字边界。

图案细节

  • \. - 一个点
  • s - sS
  • ([0-9]+) - 第1组:一个或多个数字(如果您使用([0-9]{2}),如果您认为它会更好的话,您可以将重复限制为两个)
  • e - eE
  • ([0-9]+) - ([0-9]+) - 第2组:一个或多个数字(如果您使用([0-9]{2}),如果您认为它会更好的话,您可以将重复限制为两个)
  • qazxsw poi - 一张qazxsw poi图表

\.包含第1组的内容,.包含第2组的内容。

© www.soinside.com 2019 - 2024. All rights reserved.