【问题标题】:How can I replace the contents of an element using PHP::DOMDocument?如何使用 PHP::DOMDocument 替换元素的内容?
【发布时间】:2011-05-12 06:10:34
【问题描述】:

我的文档的head 中有很多数据,但我想添加更多数据。那么如何使用PHP::DOMDocument 替换<head> 标签的内容呢?

我有以下代码:

$dom = new DOMDocument('1.0', 'UTF-8');
@$dom->loadHTML($page);

$head = $dom->getElementsByTagName('head');

$keywords = new DOMElement('meta');
keywords->setAttribute('name', 'keywords');
$keywords->setAttribute('content', $page_def['keywords']);
$head->appendChild($keywords);

$description = new DOMElement('meta');
$description->setAttribute('name', 'description');
$description->setAttribute('content', $page_def['description']);
$head->appendChild($description);

$page = $dom->saveHTML();

page::dump($page);return;

但我得到一个错误:调用未定义的方法 DOMText::setAttribute()

编辑: 我发现很难为此使用 PHP:DOMDocument。在那种情况下,我使用了以下代码:

// Add the small meta data set to the header
$page = utility::replaceStringBetween('<head>', '</head>', $header . $headers, $page);
if (utility::getReplacementCount() == 0)
{
    $error = "During page write: The head tags could not be modified within your page";
    if (settings::getConfig('context') == 'production')
    {
        if (settings::getConfig('log_enabled') == 'true')
        {
            dblog::insert(user::getId(), $error, 'error');
        }
    }
    // So the live pages will display correctly, unset the merge mode since the process which unsets it will fail to execute
    if (isset($_SESSION['merge_mode'])) unset($_SESSION['merge_mode']);

    print $error;
    return;
}

/**
* Replace the value between two other values in a string
* 
* @param string start delimeter
* @param string end delimeter
* @param string replacement
* @param string original source
* @param int limit replacement count
* @return string
*/
public static function replaceStringBetween($start, $end, $new, $source, $limit = 1)
{
    $result = preg_replace('#('.preg_quote($start) . ')(.*)('.preg_quote($end) 
        . ')#is', '$1' . $new . '$3', $source, $limit, $count);

    if ($count > 0)
    {
        self::$replacement_count++;
        return $result;
    }
    $result = preg_replace ("#{$start}(.*){$end}#is", $new, $source, $limit, $count);
    if ($count > 0)
    {
        self::$replacement_count++;
        return $result;
    }
    return $source;
}

【问题讨论】:

    标签: php html dom


    【解决方案1】:

    setAttribute() 不适用于 DOMText 节点。

    试试这个:

    $keywords = $dom->createElement('meta');
    

    【讨论】:

    • 我试过 createElement 但它不支持修改:不允许修改错误
    • $dom-&gt;createElement() 对我来说很好用。如果我使用new DOMElement(),我只会收到错误
    【解决方案2】:

    尝试使用GetElementsByTagName 找到&lt;head&gt; 标签,然后使用appendChild 添加更多标签。 . .东西。

    您也可以编辑DOMNode-&gt;$nodeValue

    【讨论】:

    • 我收到一个错误。我已经在我的问题中记录了它。任何帮助表示赞赏。
    • @Noor 尝试使用createElement 而不是createTextNode
    • 我试过 createElement 但它不支持修改:不允许修改错误
    【解决方案3】:

    但我得到一个错误:调用未定义 方法 DOMText::setAttribute()

    那是因为DOMText 类中没有方法setAttributeDOMElement中有一个。

    【讨论】:

    • 我更新了上面的代码,但出现了这个错误:使用 setAttribute 时不允许修改错误。
    猜你喜欢
    • 1970-01-01
    • 2012-10-24
    • 2011-01-15
    • 2019-02-11
    • 2019-11-19
    • 2016-11-11
    • 2018-02-18
    • 2017-12-22
    • 2020-10-22
    相关资源
    最近更新 更多