Jump to content

Str_replace does not work..


billy_111

Recommended Posts

Hey,

 

I am trying to upload a CSV file and also convert the weird symbols to their correct english format. But it does not seem to work, the CSV file successfully inserts the data, but i can't seem to get the str_replace to work..

 

This is the code:

 

    public function InsertCSVFileToDB(){

$has_title_row = true;
$not_done = array();

    if(is_uploaded_file($_FILES['csvfile']['tmp_name'])){
        $filename = basename($_FILES['csvfile']['name']);
       
        if(substr($filename, -3) == 'csv'){
            $tmpfile = $_FILES['csvfile']['tmp_name'];
            if (($fh = fopen($tmpfile, "r")) !== FALSE) {			 		
                $i = 0;
                while (($items = fgetcsv($fh, 10000, ",")) !== FALSE) {
                  	
                    if($has_title_row === true && $i == 0){ // skip the first row if there is a tile row in CSV file
                        $i++;
                        continue;
                    }
					$body = $items[2];
					$body = str_replace('Ô', ''', $body); // O symbol
					$body = str_replace('Õ', ''', $body); // O symbol
					$body = str_replace('Ð', '', $body); // D symbol
					$body = str_replace('Ž', 'f', $body); // Z symbol
					$body = mysql_real_escape_string(htmlentities($body));

                    $sql = "INSERT INTO tbl_reviews SET
                            catID='{$items[0]}',
				            title= '" . mysql_real_escape_string(htmlentities($items1])) . "', 
				            body = '$body', 
				            address= '" . mysql_real_escape_string(htmlentities($items[3])) . "', 
				            postcode='" . mysql_real_escape_string(htmlentities($items[4])) . "', 
				            tel='" . mysql_real_escape_string(htmlentities($items[5])) . "', 
				            website='" . mysql_real_escape_string(htmlentities($items[6])) . "', 
				            admission='" . mysql_real_escape_string(htmlentities($items[7])) . "', 
				            other='" . mysql_real_escape_string(htmlentities($items[8])) . "', 
				            image1='" . mysql_real_escape_string(htmlentities($items[9])) . "', 
				            image2='" . mysql_real_escape_string(htmlentities($items[10])) . "', 
				            image3='" . mysql_real_escape_string(htmlentities($items[11])) . "', 
				            image4='" . mysql_real_escape_string(htmlentities($items[12])) . "',	                            
				            date_added = now()";	
                    if(!mysql_query($sql)){
                        $not_done[] = $items;
                    }
                    $i++;
                }
            }
            // if there are any not done records found:
            if(!empty($not_done)){
                echo "<strong>There are some records could not be inserted</strong><br />";
                print_r($not_done);
            }
        }
        else{
            die('Invalid file format uploaded. Please upload CSV.');
        }
    }
    else{
        die('Please upload a CSV file.');
    }
    }

 

However the text appears like this:

 

http://freemanholland.com/babies/reviews/?ID=8

 

Scroll down to the "Imperial War Museum North" review and you will notice many weird symbols..

 

Any ideas why it does not work?

 

Thanks

 

Kind regards

Billy

Link to comment
Share on other sites

For some reason my post code has not come out properly, this section has been posted again..

 

$body = $items[2];

$body = str_replace('Ô', '&#39;', $body); // O symbol

$body = str_replace('Õ', '&#39;', $body); // O symbol

$body = str_replace('Ð', '', $body); // D symbol

$body = str_replace('&#x17D;', '&#101;', $body); // Z symbol

$body = mysql_real_escape_string(htmlentities($body));

Link to comment
Share on other sites

I have just tried adding this:

 

$body = preg_replace("/[^\x9\xA\xD\x20-\x7F]/", "", $body);

 

And it has inserted the data but removed apostrophes totally, so it displays "It's" as "Its"..

 

Which is not right..

 

Any ideas?

 

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.