Preliminary adition to transing the internet webring, and tweaks and optimization to previous code regarding javascript to php rewrite, more concise and legible code functions.
All checks were successful
Website Sync / website-sync (push) Successful in 5s
All checks were successful
Website Sync / website-sync (push) Successful in 5s
This commit is contained in:
40
includes/common_webrings.inc.php
Normal file
40
includes/common_webrings.inc.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
// Gets a file given an array with sources
|
||||
function getFile(array $source){
|
||||
$selected_source = $source["sources"][$source["selected_source"]];
|
||||
|
||||
if ($selected_source["source"] === "local") {
|
||||
$file = file_get_contents(__DIR__ . $selected_source["url"]);
|
||||
} else if ($selected_source["source"] === "internet") {
|
||||
$curl = curl_init($selected_source["url"]);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
$file = curl_exec($curl);
|
||||
|
||||
if ($file === false) {
|
||||
echo "Error: " . curl_error($curl);
|
||||
exit;
|
||||
}
|
||||
|
||||
curl_close($curl);
|
||||
}
|
||||
return $file;
|
||||
}
|
||||
|
||||
// Returns the given sub-string given a initial search pattern, and a final end pattern.
|
||||
function extractString(string $string,string $start_pattern, string $end_pattern) {
|
||||
$start_pos = strpos($string,$start_pattern,strlen($start_pattern));
|
||||
$end_pos = strpos($string,$end_pattern, $start_pos);
|
||||
|
||||
$extracted_string = substr($string, $start_pos, $end_pos - $start_pos);
|
||||
|
||||
return $extracted_string;
|
||||
}
|
||||
|
||||
// Self-explanatory
|
||||
function scrapeWebsiteSites(array $source,string $start_pattern, string $end_pattern) {
|
||||
$script = getFile($source);
|
||||
$websites = extractString($script,$start_pattern,$end_pattern);
|
||||
return $websites;
|
||||
}
|
||||
Reference in New Issue
Block a user