0

有人可以帮我让这个功能工作吗?该函数应该接受$HTMLstr——将整个 HTML 页面填充到一个字符串中,该字符串已经包含以下形式的元描述:

<meta name="description" content="This will be replaced"/>

以及$content应该替换“这将被替换”的字符串。我以为我很接近这个功能,但它并不完全有效。

function HTML_set_meta_description ($HTMLstr, $content) {
$newHTML = preg_replace('/<meta name="description"(.*)"\/>/is', "<meta name=\"description\" content=\"$content\"/>", $HTMLstr);
return ($newHTML);
}

谢谢你的帮助!

编辑:这是工作功能。

function HTML_set_meta_description ($HTMLstr, $content) {
// assumes meta format is exactly <meta name="description" content="This will be replaced"/>
$newHTML = preg_replace('/<meta name="description" content="(.*)"\/>/i','<meta name="description" content="' . $content . '" />', $HTMLstr);
return ($newHTML);

}

4

3 回答 3

1

Unless you know that the <meta> will be provided in a consistent format (which is difficult to know unless you actually have control over the HTML) you will have a very tough time constructing a working regex. Take these examples:

<meta content="content" name="description">
<meta content = 'content' name = 'description' />
<meta name= 'description' content ="content"/>

These are all valid, but the regex that would handle them would be very complex. Something like:

@<meta\s+name\s*=\s*('|")description\1\s+content\s*('|")(.*?)\2\s+/?>@

...and that doesn't even account for the attributes being in a different order. There may have been something else I didn't think of as well.

On the other hand using a parser such as DOMDocument may be very expensive, especially if your HTML is large. If you can depend on a consistent format for the <meta> you want to use .*? instead of .* to capture the content. .*? makes the search reluctant so it will stop at the first quote as opposed to the last -- there are likely to be many other quotes throughout the HTML document.

$dom = new DOMDocument;
$dom->loadHTML($HTMLstr);
foreach ($dom->getElementsByTagName("meta") as $tag) {
    if (stripos($tag->getAttribute("name"), "description") !== false) {
        $tag->setAttribute("content", $content);
    }
}
return $dom->saveHTML();
于 2013-08-30T15:53:07.373 回答
0

建议使用DOMDocument已经是一个答案,但是如果您正在努力使用正则表达式,那么我可能会帮助您。你可以试试这个:

return preg_replace('/<meta name="description" content="(.*)"\/>/i','<meta name="description" content="Something replaced" />', $HTMLstr);
于 2013-08-30T15:55:55.277 回答
0

我知道你问 preg_replace 并且我迟到了,但是看看这个,你在找它吗......

<?php
function meta_desc( $content = null ){
    $desc = 'This will be replaced ';
    if( $content ){
        $desc = $content;
    }
    return '<meta name="description"
content=" '. $desc .' "/>';
}
?>

相信我,它比那更快。我认为你应该使用这个功能。

于 2013-08-30T16:31:35.647 回答