Jump to content

Str_replace not working?


blu3t00th

Recommended Posts

Ok so I have a template system

 

Here is the a part of the template class

 

function assign_block_vars($blockname, $tags = array()){
	if(isset($this->blocks[$blockname])){
		$blockCode = $this->blocks[$blockname];
	}else{
		$blockCode = $this->get_block($blockname);
	}

	foreach ($tags as $tag => $data) {
		$blockCode = str_replace ("{".$blockname.".".$tag."}", $data, $blockCode);
	}

	$this->template = str_replace ('<!-- END '.$blockname.' -->', $blockCode.'<!-- END '.$blockname.' -->', $this->template);
}

function get_block($block){
	preg_match ('#<!-- START '. $block . ' -->(.*?)<!-- END '. $block . ' -->#s', $this->template, $this->return);
	$this->blocks[$block] = $this->return[1];
	$this->template = str_replace($this->return[1], '', $this->template);
	return $this->blocks[$block];
}

function Output(){
	return($this->template);
}

 

Here is a sample from a template file

 

				<div class="menu">
					<ul>
						<!-- START Menu -->
						<li><a href="{Menu.Linkto}">{Menu.Link}</a></li>
						<!-- END Menu -->
					</ul>
				</div>

 

Here is a sample from the page were it all happens

 

$Menu = array( 0 => array("Home", //Looping Data [Main Mavigation Tabs]
                          ROOT_PATH . "index.php",),
          	   1 => array("Register",
                      	  ROOT_PATH . "broken.php",),
               2 => array("Forums",
                      	  ROOT_PATH . "broken.php",),
               3 => array("Tutorials",
                      	  ROOT_PATH . "broken.php",),
               4 => array("Affiliates",
                      	  ROOT_PATH . "broken.php",),  	  
               5 => array("About",
                      	  ROOT_PATH . "broken.php",),                      	  
               6 => array("Contact",
                      	  ROOT_PATH . "broken.php",));

while(list($key, $value) = each($Menu)){ //Replaces results and loops
    $template->assign_block_vars("Menu", array("Link"   => $value[0],
                                               "Linkto" => $value[1]));                                                                                    
}

 

 

My problem is, When I try and replace <-- START xxxxxx --> LOOP <-- END xxxxxx --> It replaces fine, But it doesnt remove the html comment like it should, Any reason why it might not be doing that?

 

I have it a feeling it might have something to do with my replacing part

 

$this->template = str_replace ('<!-- END '.$blockname.' -->', $blockCode.'<!-- END '.$blockname.' -->', $this->template); 

 

Everything works fine, I just want the html comments removed thats all, and it dont not seem to be doing that

 

 

If you want more of the class let me know

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/103808-str_replace-not-working/
Share on other sites

Nothing seems to work :/

 

 

--

 

Idea

 

$this->template = str_replace ('<!-- END '.$blockname.' -->', $blockCode, $this->template); 
$this->template = str_replace ('<!-- START '.$blockname.' -->', $blockCode, $this->template); 

 

Does not loop, and displays double of the first entry in the array (The "Home" link)

 

--

 

Idea

 

$this->template = str_replace ('<!-- END '.$blockname.' -->', $blockCode.'<!-- END '.$blockname.' -->', $this->template);
$this->template = str_replace ('<!-- END '.$blockname.' -->', '', $this->template);
$this->template = str_replace ('<!-- START '.$blockname.' -->', '', $this->template); 

 

Once again does not loop and only displays the first entry in the array (The "Home" link)

 

--

 

The part about using

 

<!-- START xxxxxx -->

<!-- END xxxxxx -->

 

Is to show were the template class should loop

 

--

 

This function finds and replacing the variables inside the loop

        foreach ($tags as $tag => $data) {
            $blockCode = str_replace ("{".$blockname.".".$tag."}", $data, $blockCode);
        } 

 

This inserts the replaced viables

 

$this->template = str_replace ('<!-- END '.$blockname.' -->', $blockCode.'<!-- END '.$blockname.' -->', $this->template); 

 

The problem is if I get rid of the "<!-- START/END xxxxxx -->" in the above in any sort of way, it will just show the first entry because, for some reason it is removing the "<!-- START/END xxxxxx -->" before it runs the loop, therefore, not looping and only displaying the first entry in the array (because the comment is not there to signify were to do the loop)

 

--

 

Hopefully Im Making sence here...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.