Jump to content

Problems using the array from explode


joywealth

Recommended Posts

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...

 

//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);


Link to comment
https://forums.phpfreaks.com/topic/242935-problems-using-the-array-from-explode/
Share on other sites

I'll help with half:

 

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

Assuming the sentences are seperated by a line break (\n) in the text file:

$file = file_get_contents('filename.txt');
$sentences = explolde("\n",$file);

 

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

numberofwords = 5; // pick sentences with 5 words
foreach($sentences as $k=>$sentence){
$words = explode(" ",$sentence);
if(sizeof($word) != $numberofwords) unset($sentences[$k]);
}

 

 

It wasn't an homework sir. I am actually developing a application and I am just having a problem with that part. The code I posted was the part giving me problem. And Please...

 

The database is not a text file... I called it as well from database as a string. $row[]

 

Delimeter could be a period(.) or <p> or </p> because it is an html text string.

 

And Is this query alright with $publication_split% as the final array to work with?

 

 

$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

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.