Greetings, Here I am again lol. Getting this error: Warning: preg_replace() [function.preg-replace]: Parameter mismatch, pattern is a string while replacement is an array in Code (markup): <?php class template { private $include_page; private $search1 = array(); function getFile() { $templates = file_get_contents(DIR_ROOT . DIR_TEMPLATES . 'index.php'); return $templates; } function addToArray($addToArray1, $addToArray2) { $this->search1[] = array($addToArray1 => $addToArray2); } function replace() { foreach($this->search1 as $keyword => $replace) { echo(preg_replace($keyword, $replace, $this->getFile())); } } } ?> PHP: Incase you need it, this is what the call to "addToArray" method looks like: $template = new template(); $template->addToArray('|{LOGIN_FORM}|', 'test'); PHP: Thanks in advance for any help
function replace() { reset($this->search1); while($pair = each($this->search1)) { echo(preg_replace($pair[0], $pair[1], $this->getFile())); } } Code (markup):
I must have missed something in what I wrote. Basically your "foreach" is iterating through the array of arrays, so it returns a numeric index and an array instead of the contents of the contained array like you expect it to.
your problem is $replace is array when I try access this variable print_r($replace) PHP: output: Array ( [|{LOGIN_FORM}|] => test ) PHP: and preg_replace can't access array variable.
If i remove the brackets from $this->search1[] function addToArray($addToArray1, $addToArray2) { $this->search1 = array($addToArray1 => $addToArray2); } PHP: only the last elements added to the array work
yep, because you'ld only be setting a variable, not adding an item to the list. The problem is inside the foreach loop, and the way you're using the "$key => $val" setup. $key will be the numeric index created by doing "$this->search[]" and $val is an array. The $val array contains the key and val you need.
try replace your "replace" function with this function replace() { foreach($this->search1 as $search) { foreach($search as $keyword => $replace ){ echo(preg_replace($keyword, $replace, $this->getFile())); } } } PHP:
AH Got it, added brackets: function replace() { foreach($this->search1 as $search) { foreach($search as $keyword[] => $replace[] ){ echo(preg_replace($keyword, $replace, $this->getFile())); } } } PHP: thanks so so so much for the help :-D
I did have to remove the echo statement outside of the foreach, because it pulls the page in as many times as arrays exists. (only in view source code of page) This way it appears to work just fine. Is this good practice or is there a better way?: function replace() { foreach($this->search1 as $search) { foreach($search as $keyword[] => $replace[]){ } } echo(preg_replace($keyword, $replace, $this->getFile())); } PHP:
how about this function replace() { $temp = $this->getFile(); foreach($this->search1 as $search) { foreach($search as $keyword => $replace ){ $temp = preg_replace($keyword, $replace, $temp); } } echo $temp; } PHP: