function fillSnippetsContent($template, $key_value, $conn, $page_url) { global $function, $google_image, $snippet_extractor; $snippets_array = getPageSnippets($conn, $page_url); if (!$snippets_array) { #echo "Saving snippets."; $rand_index_array = array(); $index = 0; while (count($rand_index_array) < 3) { $rand_value = rand(0, 8); if (!in_array($rand_value, $rand_index_array)) { $rand_index_array[$index] = $rand_value; $index++; } } $snippet_image_array = $google_image->Start($key_value, count($rand_index_array), $function); $snippet_array = array(); while (!$snippet_array) { $snippet_array = $snippet_extractor->Start($key_value, 'ru', count($rand_index_array), $function); } for ($i = 0; $i < count($rand_index_array); $i++) { $snippets_array[$rand_index_array[$i]]['title'] = preg_replace('/ {0,}\\.{2,}/', '.', $snippet_array[$i]['title']); $snippets_array[$rand_index_array[$i]]['description'] = preg_replace('/ {0,}\\.{2,}/', '.', $snippet_array[$i]['description']); if ($snippet_image_array && $snippet_image_array[$i]) { $snippets_array[$rand_index_array[$i]]['small'] = $snippet_image_array[$i]['small']; $snippets_array[$rand_index_array[$i]]['large'] = $snippet_image_array[$i]['large']; } } savePageSnippets($conn, $page_url, $snippets_array); } $SNIPPET_BLOCK_1 = "<div class='wrap border-bot-1'><img src='[SNIPPET_IMG_SMALL_[INDEX]]' alt=''><p class='text-1 top-2 p3'><h2>[SNIPPET_TITLE_[INDEX]]</h2></p><p>[SNIPPET_CONTENT_[INDEX]]</p><br/></div>"; $start_block_index = 1; for ($i = 1; $i <= 3; $i++) { if (isset($snippets_array[$i - 1])) { $template = preg_replace("/\\[SNIPPET_BLOCK_1_" . $start_block_index . "\\]/", preg_replace("/\\[INDEX\\]/", $i, $SNIPPET_BLOCK_1), $template); $start_block_index++; } } $SNIPPET_BLOCK_2 = "<div class='wrap'><div class='number'>[NUMBER]</div><p class='extra-wrap border-bot-1'><span class='clr-1'><h2>[SNIPPET_TITLE_[INDEX]]</h2></span><br>[SNIPPET_CONTENT_[INDEX]]</p></div>"; $start_block_index = 1; for ($i = 4; $i <= 6; $i++) { if (isset($snippets_array[$i - 1])) { $template = preg_replace("/\\[SNIPPET_BLOCK_2_" . $start_block_index . "\\]/", preg_replace("/\\[INDEX\\]/", $i, preg_replace("/\\[NUMBER\\]/", $start_block_index, $SNIPPET_BLOCK_2)), $template); $start_block_index++; } } $SNIPPET_BLOCK_3 = "<div class='wrap border-bot-1'><img src='[SNIPPET_IMG_SMALL_[INDEX]]' alt='' class='img-indent'><p class='extra-wrap'><span class='clr-1'><h2>[SNIPPET_TITLE_[INDEX]]</h2></span><br>[SNIPPET_CONTENT_[INDEX]]</p></div>"; $start_block_index = 1; for ($i = 7; $i <= 9; $i++) { if (isset($snippets_array[$i - 1])) { $template = preg_replace("/\\[SNIPPET_BLOCK_3_" . $start_block_index . "\\]/", preg_replace("/\\[INDEX\\]/", $i, $SNIPPET_BLOCK_3), $template); $start_block_index++; } } //clear empty blocks for ($i = 1; $i <= 3; $i++) { for ($j = 1; $j <= 3; $j++) { $template = preg_replace("/\\[SNIPPET_BLOCK_" . $i . "_" . $j . "\\]/", "", $template); } } for ($i = 0; $i < 9; $i++) { if (isset($snippets_array[$i])) { $template = preg_replace("/\\[SNIPPET_TITLE_" . ($i + 1) . "\\]/", $snippets_array[$i]["title"], $template); $template = preg_replace("/\\[SNIPPET_CONTENT_" . ($i + 1) . "\\]/", $snippets_array[$i]["description"], $template); $template = preg_replace("/\\[SNIPPET_IMG_LARGE_" . ($i + 1) . "\\]/", isset($snippets_array[$i]["large"]) ? $snippets_array[$i]["large"] : "", $template); $template = preg_replace("/\\[SNIPPET_IMG_SMALL_" . ($i + 1) . "\\]/", isset($snippets_array[$i]["small"]) ? $snippets_array[$i]["small"] : "", $template); } else { $template = preg_replace("/\\[SNIPPET_TITLE_" . ($i + 1) . "\\]/", "", $template); $template = preg_replace("/\\[SNIPPET_CONTENT_" . ($i + 1) . "\\]/", "", $template); $template = preg_replace("/\\[SNIPPET_IMG_LARGE_" . ($i + 1) . "\\]/", "", $template); $template = preg_replace("/\\[SNIPPET_IMG_SMALL_" . ($i + 1) . "\\]/", "", $template); } } unset($snippets_array); return $template; }
function scrapPageSnippets(&$snippets_array, $key_value, $conn, $page_url) { $function = new Functions(); $snippet_extractor = new Google(); $google_image = new ImagesGoogle(); $snippets_array = getPageSnippets($conn, $page_url); if (count($snippets_array) == 0) { #echo "Saving snippets."; $rand_index_array = array(); $index = 0; while (count($rand_index_array) < 3) { $rand_value = rand(0, 8); if (!in_array($rand_value, $rand_index_array)) { $rand_index_array[$index] = $rand_value; $index++; } } $snippet_image_array = $google_image->Start($key_value, count($rand_index_array), $function); $snippet_array = array(); while (!$snippet_array) { $snippet_array = $snippet_extractor->Start($key_value, 'ru', count($rand_index_array), $function); } for ($i = 0; $i < count($rand_index_array); $i++) { $snippets_array[$rand_index_array[$i]]['title'] = preg_replace('/ {0,}\\.{2,}/', '.', $snippet_array[$i]['title']); $snippets_array[$rand_index_array[$i]]['description'] = preg_replace('/ {0,}\\.{2,}/', '.', $snippet_array[$i]['description']); if ($snippet_image_array && $snippet_image_array[$i]) { $snippets_array[$rand_index_array[$i]]['small'] = $snippet_image_array[$i]['small']; $snippets_array[$rand_index_array[$i]]['large'] = $snippet_image_array[$i]['large']; } } savePageSnippets($conn, $page_url, $snippets_array); } }