Str_replace does not work

Hey,

Any ideas why the following lines of code would not work?


						$title = str_replace('Ô', ''', $title); // O symbol
						$title = str_replace('Õ', ''', $title); // O symbol
						$title = str_replace('Ð', '', $title); // D symbol
						$title = str_replace('Ž', 'f', $title); // Z symbol
						$title = mysql_real_escape_string(htmlentities($title));

If you look on this page:

http://freemanholland.com/babies/view-reviews/?ID=3

If you look on the right, there is text that starts “Despite being…”, you will notice here that there is raw HTML and not the converted text.

Any ideas what this is?

Thanks

Hey,

Whats the html equivalent to this symbol:

Ž

I can’t seem to find the correct one…

I just have this one to convert and then i think i’m done :slight_smile:

I thought it was this:


$title = str_replace("Ž", "e", $title); // Z symbol

Is this & # 1 4 2 ; the right symbol…?

Join them up as it kept converting it as it did in the code above…

It doesn’t work for me…

You do know there is a fgetcsv function, right? :wink:

I would thinks so, the OP is using it. :stuck_out_tongue:

Am i not already using this?

I’ve been driving myself crazy with converting the data to their correct English formats.

Ok, so this fgetcsv method, can you possibly lead me in the right direction? All i want to do is insert the csv into a table, and any characters such as £ symbols, Õ, apostrophes etc read properly…

Can you help me out ?

I now have this 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;
	                    }

						$title = $items[1];
						$title = preg_replace("/[^`a-z,. \\'\\-\\d]/i", "", $title);
						$title = mysql_real_escape_string(htmlentities($title));

						$body = $items[2];
						$body = preg_replace("/[^`a-z,. \\'\\-\\d]/i", "", $body);
						$body = mysql_real_escape_string(htmlentities($body));
	
						$address = $items[3];
						$address = preg_replace("/[^`a-z,. \\'\\-\\d]/i", "", $address);
						$address = mysql_real_escape_string(htmlentities($address));

						$admission = $items[7];
						$admission = preg_replace("/[^`a-z,. \\'\\-\\d]/i", "", $admission);
						$admission = mysql_real_escape_string(htmlentities($admission));
				
						$other = $items[8];
						$other = preg_replace("/[^`a-z,. \\'\\-\\d]/i", "", $other);
						$other = mysql_real_escape_string(htmlentities($other));

	                    $sql = "INSERT INTO tbl_reviews SET
	                            catID='{$items[0]}',
					            title= '$title', 
					            body = '$body', 
					            address= '$address', 
					            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= '$admission', 
					            other='$other', 
					            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.');
	    }
    }

Can anyone help me out?

The preg_replace removes the apostrophe. I need to show the apostrophe…

:confused:

For example:


$title = str_replace("'", "Õ", $title);
$title = mysql_real_escape_string(htmlentities($title));

My csv file contains the Õ symbol in place of the apostrophe, so i need to convert the Õ to an apostrophe, which is what i thought the code above was doing?

try double quotes.

That would be the other way around:


$title = str_replace("Õ", "'", $title);
$title = mysql_real_escape_string(htmlentities($title));

Replace first parameter with second parameter in third parameter.

Nope, that doesn’t work either, see this page:

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

I tried doing this:


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

But that didn’t work either, it totally removes the apostrophes so it displays “It’s” as “Its”…

Which is not right.

how can i fix this?