3

I have some sentences. I have to choose the sentences that consist of more than 6 words. and then they will be inserted to database.

<?php
    require_once 'conf/conf.php';
    $text = " Poetry. Do you read poetry while flying? Many people find it relaxing to read on long flights. Poetry can be divided into several genres, or categories. ";
    $sentences = explode(".", $text);
    foreach ($sentences as $sentence) {
       if (count(preg_split('/\s+/', $sentence)) > 6) {
           $save = $sentence. ".";
           $sql = mysql_query("INSERT INTO tb_name VALUES('','$save')");
       }
    }
?>

The result is only the second sentence that inserted in database => 'Do you read poetry while flying? Many people find it relaxing to read on long flights'. whereas the third sentence also should be inserted. please help me, thank you : )

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
bruine
  • 647
  • 5
  • 16
  • Currently, [both strings](http://viper-7.com/oYvLsP) should be saved in the database. – nickb Jun 28 '12 at 18:32
  • @nickb yep, but only one that is saved in the database – bruine Jun 28 '12 at 18:41
  • Well the logic is set up so that `$save` is correctly set, which must mean you're running into a database issue instead of a logic issue. After `$sql = ....`, put `echo mysql_error();` to verify, and post the error message if one is produced. – nickb Jun 28 '12 at 18:42
  • Could you elaborate on the structure of your MySQL table? Could perhaps the empty string in the values be the primary key? You should be getting an error in that case, but you haven't told us that you don't – Hubro Jun 28 '12 at 18:43
  • @nickb @Codemonkey I add `if (!sql){ echo mysql_error()}` and i get an error Duplicate entry '' for key. the table is consist of two column : id and content. but all I want is to save the sentences in same rows, not in different rows. – bruine Jun 28 '12 at 18:50
  • Your delimiter is "." only. You should include "?" as a sentence delimiter. – l15a Jun 28 '12 at 18:31
  • Your delimiter is just '.'. That's why only two sentences are being inserted. What you need to do is to have '?' as a delimiter as well. – FSP Jun 28 '12 at 18:31
  • he said 2nd sentence includes `?` text and he is not splitting that into separate sentences. Last sentence 'Poetry can be ....' is not inserted – rs. Jun 28 '12 at 18:34
  • @rs yep, the problem is 'Poetry can be...' is not inserted – bruine Jun 28 '12 at 18:43

2 Answers2

3

Here is the solution you're looking for. You cannot add multiple rows since your ID value is left unspecified and it is the key into the table. Since you want to add the sentences to the same row, you need to execute one query.

$text = " Poetry. Do you read poetry while flying? Many people find it relaxing to read on long flights. Poetry can be divided into several genres, or categories. ";
$sentences = explode(".", $text); $save = array();
foreach ($sentences as $sentence) {
   if (count(preg_split('/\s+/', $sentence)) > 6) {
       $save[] = $sentence. ".";
   }
}
if( count( $save) > 0) {
    $sql = mysql_query("INSERT INTO tb_name VALUES('','" . implode( ' ', $save) . "')");
}

Now, both sentences will be inserted into the same row in the database, separated by a space. You can change what they're separated by if you modify the first parameter to implode().

The query that gets generated is this:

INSERT INTO tb_name VALUES('',' Do you read poetry while flying? Many people find it relaxing to read on long flights. Poetry can be divided into several genres, or categories.')
nickb
  • 59,313
  • 13
  • 108
  • 143
1

Replace:

$sentences = explode(".", $text);

with this:

$newSentences = array();
$sentences = preg_split("/(\.|\?|\!)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE);

$odd = false;
foreach($sentences as $sentence) {
    $sentence = trim($sentence);
    if($sentence != '') {
        if(!$odd) {
            $newSentences[] = $sentence;
        } else {
            $newSentences[count($newSentences) - 1] .= $sentence;
        }
        $odd = !$odd;
    }
}

It separates sentences ending in with . or ? or !. The foreach just reassembles the sentences.

Example here: http://codepad.org/kk3PsVGP

iDev247
  • 1,761
  • 3
  • 16
  • 36