SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need help to implement preg_replace in my import script

    Hi!
    this is my code for the import:
    Code:
         if (($handle = $source_file) !== FALSE) { 
            $columns = fgetcsv($handle, 10000, ";"); 
            foreach ($columns as &$column) { 
                $column = str_replace(".","",$column); 
            } 
            $insert_query_prefix = "INSERT INTO table (".join(",",$columns).")\nVALUES";
             while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) { 
                while (count($data)<count($columns)) 
                    array_push($data, NULL); 
                $query = "$insert_query_prefix (".join(",",quote_all_array($data)).");";
    			 mysql_query($query); 
            } 
            fclose($handle);		
        } 
    
    
    function quote_all_array($values) { 
        foreach ($values as $key=>$value) 
            if (is_array($value)) 
                $values[$key] = quote_all_array($value); 
            else 
                $values[$key] = quote_all($value); 
        return $values; 
    } 
    
    function quote_all($value) { 
        if (is_null($value)) 
            return "NULL"; 
    
        $value = "'" . mysql_real_escape_string($value) . "'"; 
        return $value; 
    }
    the value i want to remove before import is (Gå till ...) it starts with this (Gå till than more value then close with )
    so i need the preg_replace search the code were it starts with (Gå till then delete everything within that ()
    example:
    in one row this exists:
    Hey (Gå till nästa steg) mom. //the value "nästa steg" can be different

    should be after preg_replace
    Hey mom.

    after the preg_replace the sript should import the data

    i have tryed to use this kind of code but cant make it work ( dont know were to put it or how to write )

    Code:
    $pattern = '#\(\s*Gå till[^)]+\)#';
    $s = preg_replace($pattern, "", $s);
    $s = preg_replace('#\s{2,}#', " ", $s);

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    dont know were to put it
    Where is the data that has to be modified stored? In $query ?
    Then you could do:
    PHP Code:
    $query  preg_replace($pattern""$query ); 

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok i try but were in my code should i do that?

    here?
    Code:
                    array_push($data, NULL);
    
    $query  = preg_replace($pattern, "", $query ); // should i put it here before it run the mysql_query?  
    
                $query = "$insert_query_prefix (".join(",",quote_all_array($data)).");";
    			 mysql_query($query); 
            } 
            fclose($handle);
    Quote Originally Posted by guido2004 View Post
    Where is the data that has to be modified stored? In $query ?
    Then you could do:
    PHP Code:
    $query  preg_replace($pattern""$query ); 

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Immediately before you run the query:
    PHP Code:
       $query "$insert_query_prefix (".join(",",quote_all_array($data)).");";
       
    $pattern '#\(\s*Gå till[^)]+\)#';
       
    $query  preg_replace($pattern""$query ); 
       
    mysql_query($query); 
    By the way, I didn't test your regex. If you've got any problems with that, don't hesitate to ask.

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks alot i will try and come back with answer if its working or not
    Quote Originally Posted by guido2004 View Post
    Immediately before you run the query:
    PHP Code:
       $query "$insert_query_prefix (".join(",",quote_all_array($data)).");";
       
    $pattern '#\(\s*Gå till[^)]+\)#';
       
    $query  preg_replace($pattern""$query ); 
       
    mysql_query($query); 
    By the way, I didn't test your regex. If you've got any problems with that, don't hesitate to ask.

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Worked !!!
    Thanks


    Do you know any good tutorials for relational database, rdb?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •