117
- The 1st Image I use the charset=iso-8859-1
- In this 2nd image I use utf8
I have a news system where you can paste html or text from other pages.
On the page where the news are presented I use the charset=iso-8859-1
because of the accentuation but when using this charset the menus and other titles are changed due to the accentuation.
I needed your help to know if it is possible to have two charset goals on the same page or on a part of the page or other way around the situation.
In addition I have two connected files:
global $databases;
$databases = array(
'local' => array
(
'host'=>'localhost',
'port'=>3306,
'dbname'=>'noticiass',
'user'=>'root',
'password'=>''
)
);
mysql.php
Class mysql
{
public $query;
public $data;
public $result;
public $rows;
public $page = 0;
public $perpage = 10;
public $current = 1;
public $url;
public $link = '';
public $total = '';
public $pagination = false;
protected $config;
protected $host;
protected $port;
protected $user;
protected $pass;
protected $dbname;
protected $con;
public function __construct()
{
try
{
#array com dados do banco
include 'database.conf.php';
global $databases;
$this->config = $databases['local'];
# Recupera os dados de conexao do config
$this->dbname = $this->config['dbname'];
$this->host = $this->config['host'];
$this->port = $this->config['port'];
$this->user = $this->config['user'];
$this->pass = $this->config['password'];
# instancia e retorna objeto
$this->con = mysql_connect( "$this->host", "$this->user", "$this->pass" );
mysql_select_db( "$this->dbname" );
if ( !$this->con )
{
throw new Exception( "Falha na conexão MySql com o banco [$this->dbname] em database.conf.php" );
}
else
{
return $this->con;
}
$this->url = $_SERVER['SCRIPT_NAME'];
}
catch ( Exception $e )
{
echo $e->getMessage();
exit;
}
return $this;
}
public function query( $query = '' )
{
try
{
if ( $query == '' )
{
throw new Exception( 'mysql query: A query deve ser informada como parâmetro do método.' );
}
else
{
$this->query = $query;
if($this->pagination == true){
$this->result = mysql_query( $this->query );
$this->fetchAll();
$this->paginateLink();
$this->query .= " LIMIT $this->page, $this->perpage";
$this->pagination = false;
}
$this->result = mysql_query( $this->query );
}
}
catch ( Exception $e )
{
echo $e->getMessage();
exit;
}
return $this;
}
public function fetchAll()
{
$this->data = "";
$this->rows = 0;
while ( $row = mysql_fetch_array( $this->result, MYSQL_ASSOC ) )
{
$this->data[] = $row;
}
if ( isset( $this->data[0] ) )
{
$this->rows = count( $this->data );
}
return $this->data;
}
public function rowCount()
{
return @mysql_affected_rows();
}
public function getUrl($perpage)
{
$this->url = $_SERVER['REQUEST_URI'];
return $this;
}
public function paginate($perpage)
{
$this->pagination = true;
$this->perpage = $perpage;
return $this;
}
public function paginateLink()
{
if(!preg_match('/\?/',$this->url))
{
$this->url .= "?";
}else{
$this->url .= "&";
}
if ( isset( $_GET['page'] ) )
{
$this->current = $_GET['page'];
$this->page = $this->perpage * $_GET['page'] - $this->perpage;
if ( $_GET['page'] == 1 )
{
$this->page = 0;
}
}
$this->total = $this->rows;
if ( $this->rows > $this->perpage )
{
$this->link = "<div class=\"pagination\"><ul>";
$prox = "javascript:;";
$ant = "javascript:;";
if ( $this->current >= 2 )
{
$ant = $this->url."page=" . ($this->current - 1);
}
if ( $this->current >= 1 && $this->current < ($this->total / $this->perpage))
{
$prox = $this->url."page=" . ($this->current + 1);
}
$this->link .= '<li><a href="' . $ant . '">«</a></li>';
$from = round( $this->total / $this->perpage );
if($from == 1){$from++;}
for ( $i = 1; $i <= $from ; $i++ )
{
if ( $this->current == $i )
{
$this->link .= "<li class=\"active\"><a>$i</a></li>\n";
}
else
{
$this->link .= "<li><a href=\"".$this->url."page=$i\">$i</a></li>\n";
}
}
$this->link .= '<li><a href="' . $prox . '">»</a></li>';
$this->link .= "</ul>\n";
$this->link .= "</div>\n";
}
return $this;
}
public function cut($str,$chars,$info= '')
{
if ( strlen( $str ) >= $chars )
{
$str = preg_replace( '/\s\s+/', ' ', $str );
$str = strip_tags( $str );
$str = preg_replace( '/\s\s+/', ' ', $str );
$str = substr( $str, 0, $chars );
$str = preg_replace( '/\s\s+/', ' ', $str );
$arr = explode( ' ', $str );
array_pop( $arr );
//$arr = preg_replace('/\ /i',' ',$arr);
$final = implode( ' ', $arr ) . $info;
}
else
{
$final = $str;
}
return $final;
}
}
Related article : http://local.joelonsoftware.com/wiki/O_M%C3%Adnimo_absoluto_que_todo_developer_de_software_absolutely,Positivamente_Precisa_Saber_Sobre_Unicode_E_Conjuntos_de_Caracteres%28Sem_Desculpas! %29
– Guilherme Lautert
Friend, to avoid problems with charset, always set the same charset for the database (collate), for the table fields and the page header. This charset problem is common when the page is in one encoding and the database data in another. Two Charsets on the same page cannot be
– Dunga Cardoso
The universal standard of charset is utf8, for any language, do not use iso-8859-1, utf8 already interprets accentuation. In any case, you must use everything in the utf8 standard, including in the database, and the file encodes too, must be encoded in utf8, so you will stop suffering from it. And if you have iso-8859-1, convert to utf8... try conversions in this order of preference:
utf8_encode()
>html_entity_decode()
>htmlentities()
and ultimately, if none of that works, use theheader('content-type text/html charset=utf-8');
– Ivan Ferrer
And for the header of your HTML document, use the meta tag:
<meta charset="utf-8">
.– Ivan Ferrer
And before I forget, no doing this in php.ini:
php_admin_value default_charset ISO-8859-1
– Ivan Ferrer
Complicated duplicate: see more simple and objective http://answall.com/a/8442/4186 ... No doubt, use only and always UTF-8!
– Peter Krauss
@Peterkrauss what do you mean? What’s complicated about it? It is divided by topics and explained important factors, since connection, header and encoding of documents, just follow one step at a time.
– Guilherme Nascimento
I use UTF-8 where it needs and iso-8859-1 and even win-1252 where it is most convenient, because I really know well of encodings and understand the strengths and weaknesses of each, to the point of knowing that there is no solution that suits all scenarios. Unfortunately there are people who sell themselves for the silver bullet speech and think there is a universal solution. I even understand that when the guy is just a theorist it’s kind of complicated to understand these things, because one has to really study too much, and not everyone does. It is "cheaper" to adopt absolute truths.
– Bacco
I’m leaving the Joel Spolsky’s article link updated that Guilherme commented. I found a translation en for any interested party.
– fernandosavio