How can I combine everything in a string until the second separator appears with a regular expression?

I try to clarify preg_match_allby finding the second occurrence of the period, then a space:

<?php

$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; }
?>

But this does not work: the appearance {2}is wrong.

I need to use preg_match_allit because I am reading dynamic HTML.

I want to write this from a line:

East Winds 20 knots. Gusts to 25 knots.
+3
source share
6 answers

Here is a different approach

$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.
+2
source

Why not just get all the periods, and then a space, and use only some results?

preg_match_all('!\. !', $str, $matches);
echo $matches[0][1]; // second match

I'm not sure what exactly you want to get from this. Your question is a bit vague.

, , ( ), :

preg_match_all('!^((?:.*?\. ){2})!s', $str, $matches);

DOTALL, . .

, :

preg_match_all('!^((?:.*?\.(?= )){2})!s', $str, $matches);

, :

preg_match_all('!^((?:.*?\.(?: |\z)){2})!s', $str, $matches);

preg_match_all('!^((?:.*?\.(?= |\z)){2})!s', $str, $matches);

, , preg_match(), preg_match_all() .

+1

:

<?php
$str = "East Winds 20 knots. Gusts to 25 knots. Waters a moderate chop.  Slight chance of showers.";
if(preg_match_all ('/(.*?\. .*?\. )/',$str, $matches))
    $dataarrray = $matches[1];
var_dump($dataarrray);
?>

:

array(1) {
  [0]=>
  string(40) "East Winds 20 knots. Gusts to 25 knots. "
}

, , preg_match_all? preg_match .

0

, (.\s {2}) , , . , "." (, ), "."

0

.

$str = "East Winds 20 knots. Gusts to 25 knots. Waters a moderate chop.  Slight chance of showers.";
$s = explode(". ",$str);
$s = implode(". ",array_slice($s,0,2)) ;
print_r($s);
0

: East Winds 20 . 25 .

:

1) "." ( ) .

$arr = explode(".  ",$str);
echo $arr[0] . ".";
// Output: East Winds 20 knots. Gusts to 25 knots.

2) Explode Strpos, , Preg_match_all.

foreach( explode(".",$str) as $key=>$val) {
    echo (strpos($val,"knots")>0) ? trim($val) . ". " : "";
}
// Output: East Winds 20 knots. Gusts to 25 knots.
0

Source: https://habr.com/ru/post/1738434/


All Articles