在捕获单词索引时获取括号外的所有文本

时间:2018-05-05 16:41:55

标签: php regex preg-match-all

如何使用preg_match_all在括号中获取的所有文字?我需要使用preg_match_all的原因是因为我想得到每个单词的索引。

给出句子:

  

你好今天是什么时候,先生?

我可以提取里面的所有单词 ( ),这有效。我怎样才能单独在( )之外获取所有文字?

preg_match_all('/\[t-(.*?)\]/', $this->target, $targetWords, PREG_OFFSET_CAPTURE);

输出:

Array
(
    [0] => Array
        (
            [0] =>  are
            [1] => 47
        ),
    [0] => Array
        (
            [0] =>  today
            [1] => some number
        )

)

注意:我已经了解preg_split

$outsideParenthesis = preg_split('/\[.*?\]/', $this->target);

但这并不能让我维持索引。

注2:提供我的最终目标可能会有所帮助:

我想要一串自定义降价。对于每个单词,我想生成指定其类型和内容的单词对象。

原因是,我想向前端发送一个单词对象数组,这样我就可以遍历数组并生成带有类的HTML元素,所以我可以根据需要应用样式。

我希望能够指定任何降价,例如

  

你好今天[今天是什么时候],先生?

当t-是目标时,k-是关键。

所以我喜欢的最终数组看起来像是:

[
   [
      type => 'normal'
      content => 'Hello how '
   ],
   [
      type => 'target'
      content => 'are'
   ],
   [
      type => 'normal'
      content => ' you'
   ]
   [
      type => 'key'
      content => 'today'
   ]
   [
      type => 'normal'
      content => ', Sir?'
   ]
]

这是我现在的wordObjects功能:

private function setWordObjects($array, $type)
{
    return array_map(function ($n) use ($type) {
        return [
            'type' => $type,
            'content' => $n[0],
            'index' => $n[1]
        ];
    }, $array[1]);
}

2 个答案:

答案 0 :(得分:2)

使用preg_match_all

$str = 'Hello how [t- are] you [k- today], Sir?';

$types = ['' => 'normal', 't' => 'target', 'k' => 'key'];

if ( preg_match_all('~ (?| \[ (?<type>[^]-]+) - \h (?<content>[^]]+) ]
                         | () ([^[]+) ) ~x', $str, $matches, PREG_SET_ORDER) ) {
    foreach ($matches as &$m) {
        unset($m[0], $m[1], $m[2]);
        $m['type'] = $types[$m['type']];
    }
    print_r($matches);
}

demo

答案 1 :(得分:1)

扩展解决方案:

$s = 'Hello how [t- are] you [k- today], Sir?';
$types = ['t-' => 'target', 'k-' => 'key'];
$splitted = preg_split('/\[([tk]- [^]]+)\]/', $s, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE);

$result = [];
foreach ($splitted as $v) {
    [$content, $pos] = $v;
    $k = substr($content, 0, 2);
    $is_delim = isset($types[$k]);
    $result[] = array_combine(['type', 'content', 'index'],
                              [$is_delim? $types[$k] : 'normal',
                              $is_delim? substr($content, 3) : $content,
                              $is_delim? $pos + 3 : $pos]);
}

print_r($result);

输出:

Array
(
    [0] => Array
        (
            [type] => normal
            [content] => Hello how 
            [index] => 0
        )

    [1] => Array
        (
            [type] => target
            [content] => are
            [index] => 14
        )

    [2] => Array
        (
            [type] => normal
            [content] =>  you 
            [index] => 18
        )

    [3] => Array
        (
            [type] => key
            [content] => today
            [index] => 27
        )

    [4] => Array
        (
            [type] => normal
            [content] => , Sir?
            [index] => 33
        )
)