domDocument编码与数据库不匹配


domDocument encoding not match with database

我使用domDocument来查找html标签。

 $mensaje = "Te informamos que la parada <b>Plaza de la Estación</b> está
   próxima a vaciarse, el día <b>2013-04-22</b> a las <b>17:34:50</b>.";
$dom = new domDocument('1.0', 'utf8_general_ci');
          // load the html into the object ***/
          $dom->loadHTML($mensaje);
          //discard white space
          $dom->preserveWhiteSpace = false;
          $nodeList= $dom->getElementsByTagName('b'); // here u use your desired tag
          $items = array();
          for($i=0; $i < $nodeList->length; $i++) {
                    $node = $nodeList->item($i);
                    $items[] = trim($node->nodeValue);
          }
          var_dump($items);

$mensaje是从我的数据库中提取的,这个字段是utf8_general_ci,但它失败了:

array(3) { 
 [0]=> string(21) "Plaza de la Estación" 
  [1]=> string(10) "2013-04-22" 
  [2]=> string(8) "17:34:50" }

第一个元素的编码错误。

我该如何解决这个问题?

创建DOMDocument对象时指定的编码对XML文档无效。utf8_general_ci是MySQL编码。用UTF-8代替。
还要确保php文件的编码设置为UTF-8。