08-12-2015, 16:28
|
|
|
חבר מתאריך: 03.01.12
הודעות: 149
|
|
היי קובי מצורפת הפונקציה הסופית אני חושב (:
הפונקציה מחפשת דפוס חוזר של צמדים של מילים (מוגבל עד 5 מילים)
וספרת כמה פעמים הם הופיעו
בנוסף היא סופרת מילים בודדות ובודקת כמה פעמים הם הופיעו
מה אומר?
קוד PHP:
private function save_words_in_db($post_id, $content, $lang)
{
if(is_null($this->exceptionWords))
{
$this->exceptionWords = $this->getExceptionWords($lang);
}
$content = preg_replace("/[\#\,\.;&:?\[\]]+/", " ", $content);
$content = preg_replace("/\s+/", " ", $content);
//$content = preg_replace("/[^\w\-\s]/", "", $content);
$content = trim($content);
echo $content;
$insert = array();
$pattern_cache = array();
$words = explode(" ", $content);
foreach ($words as $key => $word)
{
$max_words = 0;
for ($i = 5; $i > 0; $i--)
{
if(isset($words[$key + $i]))
{
$max_words = $i;
break;
}
}
$pattern = array();
for ($i = 0; $i <= $max_words; $i++)
{
$string = '';
$j_counter = 0;
for ($j = 0; $j <= $i; $j++)
{
if(mb_strlen($words[$key + $j]) > 1)
{
if($j_counter++ > 0)
{
$string .= ' ';
}
$string .= $words[$key + $j];
}
}
$string = trim($string);
$string_md5 = md5($string);
if(!array_key_exists($string_md5, $pattern_cache))
{
$pattern[] = $string;
$pattern_cache[$string_md5] = 1;
}
}
if(!empty($pattern))
{
preg_match_all('#'.implode('|', $pattern).'#i', $content, $matchs);
foreach ($matchs as $val)
{
if(!empty($val))
{
$md5 = md5($val[0]);
if(mb_strlen($val[0]) > 2 && !array_key_exists($md5, $this->exceptionWords))
{
$insert[$md5]['blog_id'] = $post_id;
$insert[$md5]['word'] = $val[0];
$insert[$md5]['show'] = count($val);
}
}
}
}
}
echo '<pre>';print_r($insert);'<pre>';
if(!empty($insert))
{
//$this->db->insert_batch('words', $insert);
}
}
נערך לאחרונה ע"י tbe בתאריך 08-12-2015 בשעה 16:41.
|