如何使用preg_split在Forech循环中匹配字符串

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

我有2个部分|的数据,这个分隔符是这样的>>

$data = 'hello | Hello there
price | Lets talk about our support.
how are you ?| Im fine ';

我的静态单词是$word= 'price'

我的代码

 $msg = array_filter(array_map('trim', explode("\n", $data)));
 foreach ($msg as $singleLine) {
            $partition = preg_split("/[|]+/", trim($singleLine), '2');
            $part1 = strtolower($partition[0]);

            }

现在如何匹配数据..我需要结果Lets talk about our support

我有2部分的数据|像$ data ='hello这样的分隔符|您好,价格|让我们谈谈我们的支持。你好吗?|我很好 ';而我的静态单词是$ word ='price'我的代码$ msg = ...

php regex preg-match preg-match-all
2个回答
0
投票

您可以使用单个正则表达式方法:


0
投票

Wiktor的答案似乎不错,但是您可能希望将数据转换为if (preg_match('~^\h*' . preg_quote($word, '~') . '\h*\|\h*\K.*\S~m', $data, $match)) { echo $match[0]; } 数组。

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