【问题标题】:How Can I produce meta tag strings in PHP如何在 PHP 中生成元标记字符串
【发布时间】:2013-09-20 09:41:48
【问题描述】:

我想在 php 中生成我的元标记行,然后在页面中回显它们。我好像有问题。当我回显变量时,它实际上是在屏幕上回显,而不是像其他元标记一样被包含在查看源代码中。

$ogmeta = '<meta property="og:type" content="Article" />';

那我只是在做

echo $ogmeta;

我也试过

$ogmeta = htmlspecialchars('<meta property="og:type" content="Article" />');

每次回显到屏幕:(

编辑:

我发现这行得通

$ogmeta = '<meta property="og:title" content="'.$title.'" />'; 
echo $ogmeta;

但我需要有多个这样的 $ogmeta 条目:

$ogmeta = '';
$ogmeta .= '<meta property="og:title" content="'.$title.'" />';
$ogmeta .= '<meta property="og:site_name" content="some site" />';
$ogmeta .= '<meta property="og:type" content="Article" />';
$ogmeta .= '<meta property="og:url" content="'.$which_article.'" />';

当我尝试回应这一点时,它全部出现在一行上。我尝试添加换行符 但这不起作用。有什么想法吗?

【问题讨论】:

  • 你把它放在网站的头部吗?我知道这听起来很简单,但必须确保
  • 是的,它在
  • 那么你可能需要把它当作真正的来尝试。如果做不到这一点,只需填充内容而不是整个标签

标签: php tags echo meta


【解决方案1】:

如果您希望将&lt;something&gt; 视为标签,则将&amp;lt;&amp;gt; 表示为&amp;lt;&amp;gt;(它们是标签开始的HTML 字符和标签结尾)而不是 &amp;lt;&amp;gt;(它们是小于和大于的 HTML 实体)。

$ogmeta = '<meta property="og:type" content="Article">';

【讨论】:

    【解决方案2】:

    你也可以这样做。也将 PHP 插入到 &lt;meta&gt; 标记中。

    <?php
    $metaKeywords="books,cars,bikes";
    ?>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
    <title>SomePage</title>
    <meta name="description" content="<?php echo 'somedescription' ?>"></meta>
    <meta name="keywords" content="<?php echo $metaKeywords ?>"></meta>
    </head>
    

    编辑:

    解决方案一:使用HEREDOC,比较简单。

    <?php
    $metaTag=<<<EOD
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
    <title>SomePage</title>
    <meta name="description" content="my description here"></meta>
    <meta name="keywords" content="cars,bikes,books"></meta>
    </head>
    EOD;
    echo $metaTag;
    ?>
    

    解决方案 2:您还可以在 HEREDOC 中嵌入变量。

    <?php
    $metaDesc="this is new";
    $metaKeywords="cars,bikes,thrills";
    $metaTag=<<<EOD
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
    <title>SomePage</title>
    <meta name="description" content=$metaDesc></meta>
    <meta name="keywords" content=$metaKeywords></meta>
    </head>
    EOD;
    echo $metaTag;//Don't forget to echo
    ?>
    

    【讨论】:

    • 我想让整个元行 由单个变量重新表示。我试过 $ogmeta = '';但它并没有逃脱 .
    【解决方案3】:
    $ogmeta = "property='og:type' content='Article'";
    
    
    <meta <? echo $ogmeta; ?> />
    

    可以吗?

    将元标记始终保留在那里,然后填补空白。

    次佳:

    $ogmeta = "<meta property='og:type' content='Article' />";
    
    echo "$ogmeta";
    

    这应该可行。

    【讨论】:

    • 我已经想到了,但是在某些情况下 ogmeta 变量为空。
    • 这并不重要,但下一个最好的事情是(我将编辑我的帖子)
    • 最初我将元行放在单引号中。 Yu 建议使用双引号。有区别吗?
    猜你喜欢
    • 2011-10-16
    • 1970-01-01
    • 2010-09-08
    • 2010-10-26
    • 2011-06-18
    • 2011-02-06
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    相关资源
    最近更新 更多