MoFish Posted March 4, 2016 Share Posted March 4, 2016 (edited) Hi, I have created the following function but it is not working as I had hoped. I have an array of files, inside these files are placeholder values. I'm wanting to loop around the files and find all instances of the placeholders. public function findPlaceholders(){ $filenames = array("index.html", "areas.html", "testing.html"); $placeholders = array("{TITLETAG_PLACEHOLDER}","{METADESC_PLACEHOLDER}","{METAKEYWORDS_PLACEHOLDER}","{H1_PLACEHOLDER}","{AREALINKS_PLACEHOLDER}"); foreach($filenames as $filename){ // echo $filename . "<br/>"; $source = file_get_contents($this->skeleton_dir.$filename); foreach ($placeholders as $placeholder){ if(strpos($source, $placeholder)){ //echo $placeholder . "<br/>"; echo $placeholder . " found inside the file named ". $filename . "<br/>"; } } } } The function works to an extent, in that it loops around the files and writes out a single instance of the placeholder value when found, however some files have multiple placeholders which it is not displaying. Where have I gone wrong? I was hoping that it would write out all instances of the placeholders inside the files. Clearly i've got a little lost. Output {AREALINKS_PLACEHOLDER} found inside the file named areas.html{H1_PLACEHOLDER} found inside the file named testing.html Thanks, MoFish Edited March 4, 2016 by MoFish Quote Link to comment https://forums.phpfreaks.com/topic/300929-looping-around-arrays/ Share on other sites More sharing options...
Psycho Posted March 4, 2016 Share Posted March 4, 2016 Are you saying that different placeholder in the same file are not being reported or that the same placeholder in a file isn't being reported? Looking at your code, I see no reason why different placeholders would not be found. But, if the same placeholder exists in a file it will only find the first one because you are only checking once. 1 Quote Link to comment https://forums.phpfreaks.com/topic/300929-looping-around-arrays/#findComment-1531692 Share on other sites More sharing options...
MoFish Posted March 4, 2016 Author Share Posted March 4, 2016 Hi Phyco, Sometimes it just takes someone to state the obvious! I double checked the html files and it seems like the placeholder text did not save into them correctly. The functon i wrote therfore works perfectly, after a few hours scratching my head! Thank you MoFish Quote Link to comment https://forums.phpfreaks.com/topic/300929-looping-around-arrays/#findComment-1531693 Share on other sites More sharing options...
Psycho Posted March 4, 2016 Share Posted March 4, 2016 Hi Phyco, Sometimes it just takes someone to state the obvious! I double checked the html files and it seems like the placeholder text did not save into them correctly. The functon i wrote therfore works perfectly, after a few hours scratching my head! Good to hear, but as I stated, IF the same keyword exists in a file the previous code will not find it. Also, there is one small flaw that may not be consequential. strpos() will return 0 if the search term is at the beginning of the haystack string. So, if any placeholder exists at the very beginning it will not be found. The following will find multiple occurrences of a placeholder in a file and will also find ones if they are at the very beginning. public function findPlaceholders() { $filenames = array("index.html", "areas.html", "testing.html"); $placeholders = array("{TITLETAG_PLACEHOLDER}","{METADESC_PLACEHOLDER}","{METAKEYWORDS_PLACEHOLDER}","{H1_PLACEHOLDER}","{AREALINKS_PLACEHOLDER}"); foreach($filenames as $filename) { echo "<br/>Filename: {$filename}<br/>\n"; $source = file_get_contents($this->skeleton_dir.$filename); foreach ($placeholders as $placeholder) { $offset = 0; while(strpos($source, $placeholder, $offset)!==false) { $position = strpos($source, $placeholder, $offset); echo " - Placeholder {$placeholder} found inside the file named {$filename} at position {$position}<br/>\n"; $offset = $position +1; } } } } Quote Link to comment https://forums.phpfreaks.com/topic/300929-looping-around-arrays/#findComment-1531696 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.