Jump to content

need help to implement preg_replace in my import script


El Heso

Recommended Posts

Hi!

this is my code for the import:

 

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 )

 

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

Link to comment
Share on other sites

Here another solution in php.

<?php
// Given these
$needle = '(Gå till';
$curve = ')';


$string = 'Hello, You big (Gå till lotsa Bad and love) bad wonderful girl.';
$string = implode(' ', explode(substr($string, strpos($string, $needle), strpos($string, $curve)-strpos($string, $needle)+1), $string));
echo $string;

?>

Will give you.

Hello, You big bad wonderful girl.

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.