Preg 拆分或 preg 匹配以从具有已知和常量模式的字符串中提取

Preg split or preg match to extract from string with known and constant pattern

我有一个充满产品名称的数据库,所有产品名称都遵循严格且相同的命名模式,如下所示 European 222/555/111 obtained 我想 运行 一个 'for each row in table' 小 php 脚本并提取部分 222/555/111 中的 3 个子字符串并将这三个子字符串添加到单独的列中,但我没有执行 extraction.Should我用preg_split还是preg_match? 我的字符串都以单词 'European' 开头,然后是 space ,之后我需要用 / 分隔的三个选项,这些也可能只有 2 个字符,例如 European 96/43/55c strings strings

应该 $option1 = 222 $option2 = 555 $option3 = 111

我会使用 preg_match:

$string = 'European 222/555/111 obtained';

if (preg_match('~European ([^/]+)/([^/]+)/([^/\s]+)~', $string, $matches)) {
    print_r($matches);
}

输出:

Array
(
    [0] => European 222/555/111
    [1] => 222
    [2] => 555
    [3] => 111
)

解释:

~               : regex delimiter
  European\s+   : literally "European" followed by one or more space
  ([^/]+)       : match everything that is not a slash and store in group 1
  /             : a slash
  ([^/]+)       : match everything that is not a slash and store in group 2
  /             : a slash
  ([^/\s]+)     : match everything that is not a slash or a space and store in group 3
~               : regex delimiter