Problems using the array from explode

Hello Everyone,
I seek for help on this project I am working on

  1. The project requires me to explode a string (Usually a large text file) into individual sentences.

  2. Pick/Select from the array of sentences only the sentences that have at least a specified number of words. And discard the rest of the array as not useful

  3. Search my database for a string where any or as many of the sentences are contained.

  4. And then display the result…

[PHP]
//Finished getting string info
//Begin the checking process
$str = trim($row_getPublication[‘postcontent’]); // Initialize string from the Body of the Gotten Publication
array_map(‘trim’,explode(".",$str)); // Explode String
$publication_split = array_map(‘trim’,explode(".",$str)); // assign exploded string to variable
if (isset($row_getPublication[‘account_id’]))
{
$colname2_checkPublication = $row_getPublication[‘account_id’];
}
foreach ($publication_split as $each){
mysql_select_db($database_conNote, $conNote);
$query_checkPublication = sprintf(“SELECT * FROM publications WHERE postcontent LIKE %s AND account_id != %s”, GetSQLValueString("%" . $each . “%”, “text”),GetSQLValueString($colname2_checkPublication, “text”));
}

$checkPublication = mysql_query($query_checkPublication, $conNote) or die(mysql_error());
$row_checkPublication = mysql_fetch_assoc($checkPublication);
$totalRows_checkPublication = mysql_num_rows($checkPublication);

[/PHP]

Hi there,

This is how I would do it:

  1. I’m going to explode the post content and assign it to my first array variable, say $arr_1. Example:

[php]<?php
$str=“This is a very long sample sentence. This is another sentence. Another supporting sentence. Test sentence” ;
$arr_1=explode(".",$str);
?>
[/php]

So, will have this output:

Array
(
[0] => This is a very long sample sentence
[1] => This is another sentence
[2] => Another supporting sentence
[3] => Test sentence
)

  1. Create another array, say $arr_2, this time hold the values of “sentences that have at least a specified number of words”. To do this, you need to have another “explode” and have ‘space’ as separator so you’ll be able to count the number of words. Of course, the selection will be based on first array, which is the $arr_1.

For instance:

[php]$c=0;
$arr_2=array();
foreach ($arr_1 as $words){
$temp_array=explode(" ",$words);
//say am only interested to sentences with less than 3 words
if(count($temp_array)<4){
$arr_2[$c]=$words;
$c=$c+1;
}
}[/php]

  1. From $arr_2 you now have set of values needed for your search query.
  2. Do the search.

Cheers…

Now, how do how I query content of the sentences. Please help me check the part that does that from my code and point out where I am having a problem.

And Is this query alright with $publication_split as the final array to work with and % as denotes a wildcard search??

$checkNow = "SELECT * FROM `publications` WHERE `postcontent` LIKE \'%$publication_split%\' LIMIT 0, 30 ";

I tried this as well but it didn’t work. The result was:
Notice: Undefined variable: QcheckNow in C:\xampp\htdocs\writecheck.php on line 128
Query was empty

[php] $arr_1 = array_map(‘trim’,explode(".",$postcontent)); // assign exploded string to variable array.

$c=0;
$arr_2=array();

foreach ($arr_1 as $words)
{
$temp_array=explode(" ",$words);
//say am only interested to sentences with less than 3 words
if(count($temp_array)<4){
$arr_2[$c]=$words;
$c=$c+1;
}
}
$i=0;
$QcheckNow = "SELECT * FROM publications WHERE postcontent";
foreach($arr_2 as $final){
if ($i != 0) {
$QcheckNow .= " LIKE ";
}
$QcheckNow .= "’%$final%’ LIMIT 0, 30 ";
$i++;
}
}[/php]

You can’t put an array in a SELECT, because the string sending to mysql is exactly :
"SELECT * FROM publications WHERE postcontent LIKE ‘%arrayt%’ LIMIT 0, 30

and it’s not the requested results.
you must to try

$checkNow = “SELECT * FROM publications WHERE postcontent IN (”.implode(",",$publication_split).") LIMIT 0, 30";

And will that produce a wildcard search? because the sentences may be nested inside other sentences.

<?php print_r ($arr_2); ?> gives an unpleasant result in my script

Array ( [0] =>
2 [1] =>
2 [2] => 2 [3] =>
2 [4] => 2 [5] =>
2 [6] => 2 [7] => 2 [8] => 2 )

Here is my code:
[php]
$arr_1 = array_map(‘trim’,explode(".",$postcontent)); // assign exploded string to variable array.
$c=0;
$arr_2=array();
foreach ($arr_1 as $words)
{
$temp_array=explode(" ",$words);
//say am only interested to sentences with less than 3 words
if(count($temp_array)<4){
$arr_2[$c]=$words;
$c=$c+1;
}
}
[/php]

Hope this helps:

<?php $postcontent = "This is a very long sample sentence. This is another sentence. Another supporting sentence. Test sentence."; $arr_1 = explode(".",$postcontent); // assign exploded string to variable array. $c=0; $arr_2=array(); foreach ($arr_1 as $words) { $temp_array=explode(" ",$words); //say am only interested to sentences with less than 3 words if(count($temp_array)<4){ $arr_2[$c]=$words; $c=$c+1; } } foreach ($arr_2 as $search_string) { $my_variable=$search_string; //use this in your WHERE CLAUSE... /* YOUR SELECT STATEMENT HERE */ } ?>

By result shows that @$arr_2 is empty.

Sponsor our Newsletter | Privacy Policy | Terms of Service