Parameter Mismatch

Discussion in 'PHP' started by sandstorm140, Nov 28, 2008.

  1. #1
    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 :)
     
    sandstorm140, Nov 28, 2008 IP
  2. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #2
        function replace() {
            reset($this->search1);
            while($pair = each($this->search1)) {
                echo(preg_replace($pair[0], $pair[1], $this->getFile()));
            }
        }
    Code (markup):
     
    joebert, Nov 28, 2008 IP
  3. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    same error :(
     
    sandstorm140, Nov 28, 2008 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    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.
     
    joebert, Nov 28, 2008 IP
  5. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #5
    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.
     
    misbah, Nov 28, 2008 IP
  6. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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
     
    sandstorm140, Nov 28, 2008 IP
  7. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #7
    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.
     
    joebert, Nov 28, 2008 IP
  8. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I see what you mean, thanks for explaining that.
     
    sandstorm140, Nov 28, 2008 IP
  9. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Alright, I can't figure out the solution to get this working :eek::confused:. Could someone please help? Thanks :rolleyes:
     
    sandstorm140, Nov 28, 2008 IP
  10. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #10
    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:
     
    misbah, Nov 28, 2008 IP
  11. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Just the last element in the array is replaced.
     
    sandstorm140, Nov 28, 2008 IP
  12. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    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
     
    sandstorm140, Nov 28, 2008 IP
  13. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    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:
     
    sandstorm140, Nov 28, 2008 IP
  14. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #14
    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:
     
    misbah, Nov 28, 2008 IP
  15. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    that works perfectly, thank you :)
     
    sandstorm140, Nov 29, 2008 IP