【问题标题】:Better automated html meta tags with php使用 php 更好地自动化 html 元标记
【发布时间】:2015-03-19 21:40:45
【问题描述】:

我需要以更好的方式执行此操作,或者修复我遇到的错误。 错误:解析错误:语法错误,意外的 '=>' (T_DOUBLE_ARROW),在第 15 行的 C:\xampp\htdocs\php\config.php 中需要 ')'

<?php
    $meta_build = "<head>";
    $meta = array(
                array(
                    'type' => 'title',
                    'content' => 'Test'
                ),

                array(
                    'type' => 'meta',
                    'name' => 'description',
                    'content' => 'Somestuff',
                ),

                array(
                    'type' => 'meta',
                    'name' => 'viewport',
                    'content' => 'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no'
                ),

                array(
                    'type' => 'text/css',
                    'link' => 'http://fonts.googleapis.com/css?family=Titillium+Web'
                ),

                array(
                    'type' => 'css',
                    'link' => '/css/bootstrap.min.css'
                ),

                array(
                    'type' => 'css',
                    'link' => '/css/bootstrap-theme.min.css'
                ),

                array(
                    'type' => 'css',
                    'link' => '/css/main.css'
                )

        );
    foreach($meta as $item){
        if($item['type'] == 'title'){
            $meta_build .= "<title>";
            $meta_build .= $item['content'];
            $meta_build .= "</title>";
        }

        if($item['type'] == 'meta'){
            $meta_build .= "<meta";
            $meta_build .= 'name="';
            $meta_build .= $item['name'].'" content="';
            $meta_build .= $item['content'].'"/>';
        }

        if($item['type'] == 'text/css'){
            $meta_build .= '<link rel="stylesheet"';
            $meta_build .= 'href="'.$item['link'].'"';
            $meta_build .= "type='text/css'/>";
        }

        if($item['type'] == 'css'){
            $meta_build .= '<link rel="stylesheet"';
            $meta_build .= 'href="'.$item['link'].'"/>';
        }

    }


    $meta_build .= "</head>";
    echo $meta_build;
?>

【问题讨论】:

  • 我执行了你的代码,使用 php 5.4.4 和 error_reporting(E_ALL) 得到了零错误。
  • 在构建标签时可能需要放置空格。示例:'
  • 你显示的文件也是config.php吗?
  • 不,没有帮助。也许你可以帮我写一个有效的方法来用 php 构建我的元标记。不,该文件称为 head.php。耶稣我瞎了……或者我看不懂。

标签: php html meta


【解决方案1】:

如果您像这样修改您的 foreach,您将有适当的空格和每个标签的新行。

foreach($meta as $item){
    if($item['type'] == 'title'){
        $meta_build .= "<title>";
        $meta_build .= $item['content'];
        $meta_build .= "</title>";
    }

    else if($item['type'] == 'meta'){
        $meta_build .= "<meta ";
        $meta_build .= 'name="';
        $meta_build .= $item['name'].'" content="';
        $meta_build .= $item['content'].'"/>';
    }

    else if($item['type'] == 'text/css'){
        $meta_build .= '<link rel="stylesheet" ';
        $meta_build .= 'href="'.$item['link'].'" ';
        $meta_build .= "type='text/css'/>";
    }

    else if($item['type'] == 'css'){
        $meta_build .= '<link rel="stylesheet" ';
        $meta_build .= 'href="'.$item['link'].'"/>';
    }
    $meta_build .= "\n";
}

【讨论】:

  • 谢谢史蒂文。现在它正在按我的意愿工作。我不知道“\n”的事情。
  • @Urahara 我谈谈添加的\n
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-25
  • 2017-05-29
  • 2011-03-02
  • 2012-05-08
  • 1970-01-01
  • 2017-04-23
  • 2011-11-10
相关资源
最近更新 更多