Viewed   95 times

I need to split a paragraph into sentences. That's where i got a bit confused with the regex.

I have already referred this question to which this Q is marked as a duplicate to. but the issue here is different.

Here is a example of the string i need to split :

hello! how are you? how is life
live life, live free. "isnt it?"

here is the code i tried :

$sentence_array = preg_split('/([.!?rn|r|n])+(?![^"]*")/', $paragraph, -1);

What i need is :

array (  
  [0] => "hello"  
  [1] => "how are you"  
  [2] => "how is life"  
  [3] => "live life, live free"  
  [4] => ""isnt it?""  
)

What i get is :

array(
  [0] => "hello! how are you? how is life live life, live free. "isnt it?""
)

When i do not have any quotes in the string, the split works as required.

Any help is appreciated. Thank you.

 Answers

3

There are some problems with your regular expression that the main of them is confusing group constructs with character classes. A pipe | in a character class means a | literally. It doesn't have any special meaning.

What you need is this:

("[^"]*")|[!?.]+s*|R+

This first tries to match a string enclosed in double quotation marks (and captures the content). Then tries to match any punctuation marks from [!?.] set to split on them. Then goes for any kind of newline characters if found.

PHP:

var_dump(preg_split('~("[^"]*")|[!?.]+s*|R+~', <<<STR
hello! how are you? how is life
live life, live free. "isnt it?"
STR
, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));

Output:

array(5) {
  [0]=>
  string(5) "hello"
  [1]=>
  string(11) "how are you"
  [2]=>
  string(11) "how is life"
  [3]=>
  string(20) "live life, live free"
  [4]=>
  string(10) ""isnt it?""
}
Friday, December 9, 2022
 
1
<?php

$pattern = '/[;,]/';

$string = "something here ; and there, oh,that's all!";

echo '<pre>', print_r( preg_split( $pattern, $string ), 1 ), '</pre>';

Updated answer to an updated question:

<?php

$pattern = '/[x{ff0c},]/u';

//$string = "something here ; and there, oh,that's all!";
$string = 'hei,nihao?a ';


echo '<pre>', print_r( preg_split( $pattern, $string ), 1 ), '</pre>';
Friday, November 25, 2022
2

Use a callback, you can detect which pattern matched by using capturing groups, like (?:(patternt1)|(pattern2)|(etc), only the matching patterns capturing group(s) will be defined.

The only problem with that is that your current capturing groups would be shifted. To fix (read workaround) that you could use named groups. (A branch reset (?|(foo)|(bar)) would work (if supported in your version), but then you'd have to detect which pattern has matched using some other way.)

Example

function replace_callback($matches){
    if(isset($matches["m1"])){
        return "foo";
    }
    if(isset($matches["m2"])){
        return "bar";
    }
    if(isset($matches["m3"])){
        return "baz";
    }
    return "something is wrong ;)";
}

$re = "/(?|(?:regex1)(?<m1>)|(?:reg(\s*)ex|2)(?<m2>)|(?:(back refs) work as intended \1)(?<m3>))/";

$rep_string = preg_replace_callback($re, "replace_callback", $string);

Not tested (don't have PHP here), but something like this could work.

Thursday, August 11, 2022
 
2

But there is a solution for your crazy wish ;)

$a = "(Z) X, (Y, W) A, B (one, two), C, D (E,F,G) H, I J";
$reg = '/[^(,]*(?:([^)]+))?[^),]*/';
preg_match_all($reg, $a, $matches);
$result = array_filter($matches[0]);
var_dump($result);
Tuesday, December 6, 2022
 
sacha
 
1

Thanks to php.net I've come up with a solution : you have to use (mysqli_multi_query($link, $query)) to run multiple concatenated queries.

 /* create sql connection*/
$link = mysqli_connect("server", "user", "password", "database");

$query = "SQL STATEMENTS;"; /*  first query : Notice the 2 semicolons at the end ! */
$query .= "SQL STATEMENTS;"; /* Notice the dot before = and the 2 semicolons at the end ! */
$query .= "SQL STATEMENTS;"; /* Notice the dot before = and the 2 semicolons at the end ! */
$query .= "SQL STATEMENTS"; /* last query : Notice the dot before = at the end ! */

/* Execute queries */

if (mysqli_multi_query($link, $query)) {
do {
    /* store first result set */
    if ($result = mysqli_store_result($link)) {
        while ($row = mysqli_fetch_array($result)) 

/* print your results */    
{
echo $row['column1'];
echo $row['column2'];
}
mysqli_free_result($result);
}   
} while (mysqli_next_result($link));
}

EDIT - The solution above works if you really want to do one big query but it's also possible to execute as many queries as you wish and execute them separately.

$query1 = "Create temporary table A select c1 from t1"; 
$result1 = mysqli_query($link, $query1) or die(mysqli_error());

$query2 = "select c1 from A"; 
$result2 = mysqli_query($link, $query2) or die(mysqli_error());

while($row = mysqli_fetch_array($result2)) {

echo $row['c1'];
    }  
Monday, August 22, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :