• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replacing special characters with preg_replace

[eluser]Constantin Iliescu[/eluser]
Hello, forum!

This may seem an easy one for many... I'm struggling with it for a few hours now...
I have a database which initially had collation latin1_swedish. It had some data inserted into it's tables (also latin1_swedish). I'm talking about the database for a multilanguage site, so the best collation, as far as I know, is utf8_general_ci.
The text inside one field looks like this: "Facultatea de Economie ÅŸi Administrarea". I want to replace "ÅŸ" with "ș". I can, if I give this text directly as the third parameter to the preg_replace function, but when I get if from the database field, preg_replace doesn;t do anything to it.

Here is some code:

This one works:
$text = 'Facultatea de Economie ÅŸi Administrarea Afacerilor.';
echo preg_replace('/ÅŸ/', 'ș', $text);

but when I try this:

$tables = array();
   $tables_with_fields = array();

   $link_id = mysql_connect('localhost', 'root', '') or die('Error establishing a database connection');
   mysql_select_db('feaa_site', $link_id);

   $resource = mysql_query("SHOW TABLES", $link_id);
   while ( $result = mysql_fetch_row($resource) )
      $tables[] = $result[0];

   if ( !empty($tables) )
      foreach ( (array) $tables as $table )
         $resource = mysql_query("EXPLAIN $table", $link_id);
         while ( $result = mysql_fetch_assoc($resource) )
            if ( preg_match('/(char)|(text)|(enum)|(set)/', $result['Type']) )
               $res = mysql_query("SELECT " . $result['Field'] . " FROM " . $table, $link_id);
               while ($r = mysql_fetch_row($res))
                  echo preg_replace("/ÅŸ/", "ș", $r[0]);

The text is displayed just like it's saved in the database...

Any ideas how I could solve this?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.