带有固定字符的完整HTML

时间:2018-09-03 07:43:52

标签: php html special-characters

我使用此功能来完成HTML。

function correct_html($string) {
    $dom = new \DOMDocument;
    $dom->loadHTML($string);

    $mock = new \DOMDocument;
    $body = $dom->getElementsByTagName('body')->item(0);
    foreach ($body->childNodes as $child) {
        $mock->appendChild($mock->importNode($child, true));
    }

    return trim($mock->saveHTML());
}

但是当我将此功能与é或任何其他特殊字符一起使用时,它会给我é。我该如何解决

2 个答案:

答案 0 :(得分:0)

我认为您应该在DOMDocument的结构中添加编码。

  createYg() {
    return this._fb.group({
      subfield: '',
      Z: this._fb.array([])
    });
  }

  createZg() {
    return this._fb.group({
      subsubfield: ''
    });
  }

还要确保在标题中设置了编码。

$dom = new DOMDocument('1.0', 'UTF-8');

答案 1 :(得分:0)

尝试添加

  

utf8_decode()

像这样将字符串解析为loadHTML()之前,

function correct_html($string) {
    $dom = new \DOMDocument();
    $dom->loadHTML(utf8_decode($string));

    $mock = new \DOMDocument;
    $body = $dom->getElementsByTagName('body')->item(0);
    foreach ($body->childNodes as $child) {
        $mock->appendChild($mock->importNode($child, true));
    }

    return trim($mock->saveHTML());
}