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
https://forums.phpfreaks.com/topic/204757-str_replace-does-not-work/
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));

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.