How to split text to combine double quotes plus trailing text to a point?

How can I get a sentence that is in double quotes, at which there is a dot that needs to be divided?

An example document is as follows:

“Chess helps us overcome difficulties and suffering,” said Unikrishnan, taking my queen. On the chessboard, you fight, because we also struggle with the difficulties in our daily lives, "he said.

I want to get the result as follows:

Array
(
    [0] =>"Chess helps us overcome difficulties and sufferings," said Unnikrishnan, taking my queen.
    [1] =>"On a chess board you are fighting. as we are also fighting the hardships in our daily life," he said.
 )

My code is still exploding in dots.

function sample($string)
{
    $data=array();
    $break=explode(".", $string);
    array_push($data, $break);

    print_r($data);
}

I'm still confused to separate the two delimiters into a double quote and a dot. because there is a sentence inside the double quote that contains a dot delimiter.

+6
3

, preg_split(), preg_replace(), (Demo)

$in='"Chess helps us overcome difficulties and sufferings," said Unnikrishnan, taking my queen. "On a chess board you are fighting. as we are also fighting the hardships in our daily life." he said.';

$out=preg_split('/ (?=")/',$in,null,PREG_SPLIT_NO_EMPTY);

$find='/[""]/u';  // unicode flag is essential
$replace='"';
$out=preg_replace($find,$replace,$out);  // replace curly quotes with standard double quotes

var_export($out);

:

array (
  0 => '"Chess helps us overcome difficulties and sufferings," said Unnikrishnan, taking my queen.',
  1 => '"On a chess board you are fighting. as we are also fighting the hardships in our daily life." he said.',
)

preg_split() , " (LEFT DOUBLE QUOTE).

preg_replace() u, , . '/"|"/' , u, , ( 189 372 ).

preg_split() preg_match_all(), preg_split() , , left double quote. preg_match_all() , , , .

, preg_match_all(), preg_split() :

$out=preg_match_all('/".+?(?= "|$)/',$in,$out)?$out[0]:null;
0

(*SKIP)(*FAIL):

"[^""]+"(*SKIP)(*FAIL)|\.\s*
# looks for strings in double quotes
# throws them away
# matches a dot literally, followed by whitespaces eventually


PHP:
$regex = '~"[^""]+"(*SKIP)(*FAIL)|\.\s*~';
$parts = preg_split($regex, $your_string_here);

Array
(
    [0] => "Chess helps us overcome difficulties and sufferings," said Unnikrishnan, taking my queen
    [1] => "On a chess board you are fighting. as we are also fighting the hardships in our daily life."
)

regex101.com, ideone.com.

+2

:

regex101 (16 )

".[^"]+"(?:.[^"]+)?

  • ".[^"]+" " ".
  • (?:.[^"]+)?- it is an opportunity, so the last - all that is not starting , is not an exciting group. ? " ?:

PHP - PHPfiddle : - Hit "Run-F9" - [updated to replace , with ] " " "

<?php
    $str = '"Chess helps us overcome difficulties and sufferings," said Unnikrishnan, taking my queen. "On a chess board you are fighting. as we are also fighting the hardships in our daily life."';

if(preg_match_all('/".[^"]+"(?:.[^"]+)?/',$str, $matches)){
    echo '<pre>';
    print_r(preg_replace('["|"]', '"', $matches[0]));
    echo '</pre>';
}
?>

exit:

Array
(
    [0] => "Chess helps us overcome difficulties and sufferings," said Unnikrishnan, taking my queen. 
    [1] => "On a chess board you are fighting. as we are also fighting the hardships in our daily life."
)
0
source

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


All Articles