我试图通过查找第二个出现的句点然后一个空格来完善preg_match_all:
$str = "East Winds 20 knots. Gusts to 25 knots. Waters a moderate chop. Slight chance of showers.";
preg_match_all ('/(^)((.|\n)+?)(\.\s{2})/',$str, $matches);
$dataarray=$matches[2];
foreach ($dataarray as $value)
{ echo $value; }
?>
但是它不起作用:{2}出现是不正确的.
我必须使用preg_match_all,因为我正在抓取动态HTML.
我想从字符串中捕获它:
East Winds 20 knots. Gusts to 25 knots.
解决方法:
这是一种不同的方法
$str = "East Winds 20 knots. Gusts to 25 knots. Waters a moderate chop. Slight chance of showers.";
$sentences = preg_split('/\.\s/', $str);
$firstTwoSentences = $sentences[0] . '. ' . $sentences[1] . '.';
echo $firstTwoSentences; // East Winds 20 knots. Gusts to 25 knots.
标签:preg-match-all,php,regex
来源: https://codeday.me/bug/20191210/2099775.html