Jump to content

Looping Around Arrays


MoFish

Recommended Posts

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 by MoFish
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;
            }
        }
    }
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.