I have tried to write a large string (~100-120 Kb of HTML) on an md
file, and am pretty sure it's not the fastest method, even though it only has to iterate ~8000-10,000 times and few times per hour.
There is also a low (~1%-2%) probability that the target filename has an old name (previousName
), not exactly matched with a new name (newName
), because the data flows through an API.
Key Script: Inside For
Loop
$cn=strtolower(UpdateStocks::slugCompany($s["quote"]["companyName"])); // slug company
$hay=strtolower($arr["quote"]["primaryExchange"]); // exchange market
if(strpos($hay, 'nasdaq')===0){
$mk='nasdaq-us';
$nasdaq++;
}elseif(strpos($hay, 'nyse')===0 || strpos($hay, 'new york')===0){
$mk='nyse-us';
$nyse++;
}elseif(strpos($hay, 'cboe')===0){
$mk='cboe-us';
$cboe++;
}else{
$mk='market-us';
$others++;
}
$sc=str_replace(array(' '), array('-'), strtolower($s["quote"]["sector"])); // slug sector
$enc=UpdateStocks::getEnc($symb,$symb,$symb,self::START_POINT_URL_ENCRYPTION_APPEND, self::LENGTH_URL_ENCRYPTION_APPEND); // simple 4 length encryption output: e.g., 159a
$dir=__DIR__ . self::DIR_FRONT_SYMBOLS_MD_FILES; // symbols front directory
if(!is_dir($dir)){mkdir($dir, 0755,true);} // creates price targets directory if not exist
// symbol in url
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
// company in url
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
// duplication risk
if(strtolower($symb)===strtolower($cn)){
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p2));
$lurl=$cn . '-' . $sc . '-' . $mk . '-' . $enc;
}else{
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p1));
$lurl=strtolower($symb) . '-' . $cn . '-' . $sc . '-' . $mk . '-' . $enc;
}
// new md filename
$newName=$dir . self::SLASH . $lurl . self::EXTENSION_MD;
// Replace multiple dashes with single dash: "aa-alcoa-basic-materials-nyse-us-159a"
$newName = preg_replace('/-{2,}/', '-', $newName);
// if file not exist: generate file
if($previousNames==null){
$fh=fopen($newName, 'wb');
fwrite($fh, '');
fclose($fh);
}else{
// if file not exist:
foreach($previousNames as $k=>$previousName){
if($k==0){
// safety: if previous filename not exactly equal to new filename
rename($previousName, $newName);
}else{
// in case multiple files found: unlink
unlink($previousName);
}
}
}
// This method is not review required now.
$mdFileContent=UpdateStocks::getBaseHTML($s,$l,$z); // gets HTML
if(file_exists($newName)){
if(is_writable($newName)){
file_put_contents($newName,$mdFileContent);
echo $symb. " 💚 " . self::NEW_LINE;
}else{
echo $symb . " symbol file in front directory is not writable in " . __METHOD__ . " 💔" . self::NEW_LINE;
}
}else{
echo $symb . " file not found in " . __METHOD__ . " 💛" . self::NEW_LINE;
}
}
searchFilenames
/**
*
* @return an array with values of paths of all front md files stored
*/
public static function searchFilenames($array,$re){
$arr= array();
foreach($array as $k=>$str){
$pos=strpos($str, $re);
if($pos!==false){
array_push($arr, $str);
}
}
return $arr;
}
var_dump($hay)
string(20) "nasdaq global"
string(23) "new york stock exchange"
string(20) "nasdaq global market"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(23) "nyse arca"
string(23) "new york stock exchange"
string(23) "nyse"
string(20) "nasdaq"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(23) "cboe"
string(23) "new york stock exchange"
string(20) "nasdaq global select"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(23) "new york stock exchange"
...
>5% Probability of var_dump($lurl)
For strtolower($symb)===strtolower($cn)
string(27) "aac-healthcare-nyse-us-e92a"
string(35) "aaon-basic-materials-nasdaq-us-238e"
string(28) "abb-industrials-nyse-us-a407"
string(38) "acnb-financial-services-nasdaq-us-19fa"
<95% Probability of var_dump($lurl)
For not strtolower($symb)===strtolower($cn)
string(50) "aadr-advisorshares-dorsey-wright-adr--nyse-us-d842"
string(39) "aal-airlines-industrials-nasdaq-us-29eb"
string(68) "aamc-altisource-asset-management-com-financial-services-nyse-us-b46a"
string(47) "aame-atlantic-financial-services-nasdaq-us-8944"
string(35) "aan-aarons-industrials-nyse-us-d00e"
string(54) "aaoi-applied-optoelectronics-technology-nasdaq-us-1dee"
string(56) "aap-advance-auto-parts-wi-consumer-cyclical-nyse-us-1f60"
string(36) "aapl-apple-technology-nasdaq-us-8f4c"
string(35) "aat-assets-real-estate-nyse-us-3598"
string(49) "aau-almaden-minerals-basic-materials-nyse-us-1c57"
string(51) "aaww-atlas-air-worldwide-industrials-nasdaq-us-69f3"
string(59) "aaxj-ishares-msci-all-country-asia-ex-japan--nasdaq-us-c6c4"
string(47) "aaxn-axon-enterprise-industrials-nasdaq-us-0eef"
string(58) "ab-alliancebernstein-units-financial-services-nyse-us-deb1"
$symb
:
Is a uppercase string, stands for a "symbol" of an equity, sometimes with dashes.
"AADR"
"AAL"
"AAMC"
"AAME"
"AAN"
"AAOI"
"AAP"
"AAPL"
"AAT"
"AAU"
"AAWW"
"AAXJ"
"AAXN"
"AB"
"GS-A"
"GS-B"
"GS-C"
Would you be so kind and help me to modify it with a faster/simpler script?
substr($hey, 0,2)
once and then check the first two letters instead of multiplestrpos
. Maybe, but I have no idea what$hey
looks like :) - but than you could switch on that and get rid of the multiple function calls. I don't think it will be much faster, but with enough iterations, who knows, and it may look a bit cleaner. \$\endgroup\$$symb
is / is doing. \$\endgroup\$$symb
? (If you "ping" me on pages where I have not been active, I will not be alerted.) \$\endgroup\$