Jump to content

Foreign Characters with PHP and MySQL


imperium2335

Recommended Posts

How do I convert accented characters etc into their code form so I can put them into my database? I have tried everything I can think of like htmlentities, htmlspecialchars, url_decode etc and nothing seems to work!

 

I am storing content in a table with different fields for different languages, e.g. polish, spanish etc.

I can use the following function:

function getRewriteString($sString) {
     $string = strtolower(htmlentities($sString));
     $string = preg_replace("/&(.)(uml);/", "$1e", $string);
     $string = preg_replace("/&(.)(acute|cedil|circ|ring|tilde|uml);/", "$1", $string);
     $string = preg_replace("/([^a-z0-9]+)/", " ", html_entity_decode($string));
     $string = trim($string, "-");
     return $string;
  }

Which replaces accented chars with their unaccented counterparts but it doesn't work 100% of the time and isn't really what I want because I'd like to have the accented chars as they will be used on different language sites.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/211604-foreign-characters-with-php-and-mysql/
Share on other sites

if you work in Unicode, you will not have to worry about this.

Have a look here:

http://www.richnetapps.com/php-mysql-speak-unicode/

 

Basically,

1. use the mb_ string functions in PHP

2. make sure your DB uses the Unicode charset

3. make sure your HTML has the correct UTF-8 header

 

If you do all of this... you will not have to convert anything

Thanks, but it didn't work.

 

The database is now being populated with things like this: S5 część plc

 

this is the source:

OpisS5 część plcadapter kąta prostego 9-polarnychSiemens S7 modułSiemens S7 modułS5 plc 135/155 32 kanałowy moduł wejścia 24VDCSystem 3 wejścia modułuStep7 zawodowych susStandardowych dysków 200vfsa0.37kw-0.75kWSimatic C7-613 kompaktowySiemens dysk najlepsze kartyS7-200 em cyfrowe wejście 221zakresie Simatic pg PIII 1GHz win2000SINAMICS g110 analogowe 200V 1Ph 0.75kWModuł EpromSiemens 6ra płyty analogowe DCS5 plc 135 / 155 16 ch. 24 - 60vdc moduł wejśćSimatic pcs7 v6.0 dokumentacji cdS5 część plcSinumerik 820/850/880 klawiatury panelu operatora

Db is set to unicode, i have header('Content-Type: text/html;charset="utf-8"'); at the top on my script, and i have the languages being set to unicode to:

echo $polish    = htmlentities(translate($desc, "pl"), ENT_QUOTES, 'utf-8') ;

 

I guess you forgot this then:

$result = mysql_query('SET NAMES utf8');

$result = mysql_query('SET CHARACTER SET utf8');

 

read the whole page !!

ok here it is:

<?PHP
header('Content-Type: text/html;charset="utf-8"');

function translate( $text, $destLang, $srcLang = 'en' ) { // Converting to English.

$text = urlencode( $text );
$destLang = urlencode( $destLang );
$srcLang = urlencode( $srcLang );

$trans = @file_get_contents( "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q={$text}&langpair={$srcLang}|{$destLang}" );
$json = json_decode( $trans, true );

if( $json['responseStatus'] != '200' ) return false; else return $json['responseData']['translatedText'];
}

$columns = 3+1 ;

$file = "C:\Users\TCWH\Desktop\siemens-parts.csv" ;

$fh = fopen($file, 'r') ;

$data = fread($fh, filesize($file)) ;

//$commas = ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,," ;

$rows = explode("\r", $data) ;

include("dbconnect.php") ;

$query = "SELECT EquipmentID FROM parts_table ORDER BY EquipmentID DESC" ;
$result = mysql_query('SET NAMES utf8');
$result = mysql_query('SET CHARACTER SET utf8');
$result = mysql_query($query) ;

$row = mysql_fetch_assoc($result) or die(mysql_error()) ; 

$rowstart = $row['EquipmentID'] + 1 ;

foreach($rows as $current) {

$column = explode(',', $current, 4) ;

$category = $column[0] ;
$partno = $column[1] ;
$desc = ucfirst(strtolower($column[2])) ;

//$french    = htmlentities((utf8_decode(translate($desc, "fr"))), ENT_QUOTES, 'utf-8') ;
//$german    = htmlentities((utf8_decode(translate($desc, "de"))), ENT_QUOTES, 'utf-8') ;
echo $polish    = htmlentities(translate($desc, "pl"), ENT_QUOTES, 'utf-8') ;
//$spanish   = htmlentities(translate($desc, "es"), ENT_QUOTES, 'utf-8') ;
//$italian   = htmlentities((utf8_decode(translate($desc, "it"))), ENT_QUOTES, 'utf-8') ; 
//$arabic  = translate($desc, "ar") ;
//$chinese = translate($desc, "zh") ;
//$port    = translate($desc, "pt") ;
//$cz      = translate($desc, "cz") ;

$query = "INSERT INTO parts_table (EquipmentID, CategoryID, ManufacturerID, PartNumber, DetailURL) VALUES
('$rowstart', '$category', '$manu', '$partno', '$URL')" ;
$result = mysql_query('SET NAMES utf8');
$result = mysql_query('SET CHARACTER SET utf8');
$result = mysql_query($query) or die(mysql_error()) ;

$query = "INSERT INTO languagecontent (ContentID, ContentEn, ContentFr, ContentEs, ContentDe, ContentPl) VALUES ('$rowstart', '$desc', '$french', '$spanish', '$german', '$polish')" ;
$result = mysql_query('SET NAMES utf8');
$result = mysql_query('SET CHARACTER SET utf8');
mysql_query($query) or die(mysql_error()) ;

if($column[0] == "" or $column[0] == NULL) break ;

$rowstart++ ;

}
//print_r($french) ; echo "<br />" ;
//print_r($german) ; echo "<br />" ;
//print_r($polish) ; echo "<br />" ;
//print_r($spanish) ; echo "<br />" ;
//print_r($italian) ; echo "<br />" ;
//print_r($arabic) ; echo "<br />" ;

?>

Ok...

So I did a little research and came across this site:

http://verens.com/2006/05/05/some-utf8-problems-and-solutions/

 

Where it says:

A lot of further work revealed that, in fact, the default mysql_* functions simply cannot handle character sets very well. Instead, you must use the mysqli_* functions. There is simply no simple way around this!

 

Could you give it a try with the mysqli functions ?

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.