【问题标题】:PHP Title, Description and Keywords Per Page每页的 PHP 标题、描述和关键字
【发布时间】:2012-08-31 00:45:27
【问题描述】:

我正在尝试找出一种方法来为每页创建描述和关键字。

标题应该是:

{{title=some page title in here}}

对于描述,我会这样做:

{{description=some description per page in here}}

对于关键字元标记,我会这样做:

{{keywords=example keyword, per each page, this is an example}}

我将如何通过 preg_replace + regex 解析来实现这一点,因此它不会在它自己的页面上可见,而是放置在实际的元信息中,例如:

<title> some page title in here </title>
<meta name="description" content="some description per page in here">
<meta name="keywords" content="example keyword, per each page, this is an example">

示例页面如下所示:

{{title=some page title in here}}
{{description=some description per page in here}}
{{keywords=example keyword, per each page, this is an example}}

<div id="content">
  <h4> Some page title here </h4>
  <p> Some page paragraphs here. </p>
</div> <!--#content-->

当然结果与此类似:

<html>
<head>
  <title> Website Title - some page title in here </title>
  <meta name="description" content="some description per page in here">
  <meta name="keywords" content="example keyword, per each page, this is an example">
</head>
<body>
  <div id="content">
    <h4> Some page title here </h4>
    <p> Some page paragraphs here. </p>
  </div> <!--#content-->
</body>
</html>

非常感谢您的帮助。

【问题讨论】:

  • 我觉得 jQuery 会是这样的更好的解决方案...
  • 我不确定我是否理解您的目标。能详细点吗?
  • 对于 {{title=some page title in here}} 和 {{description=some description per page in here}} 和 {{keywords=example keyword, per each page, this是一个例子}}来做这个解析

标签: php regex seo


【解决方案1】:

如果我没看错,你想包括这样的东西:

<title><?php echo $page_title; ?></title>

页面标题在脚本前面设置的位置

【讨论】:

  • 你能给出一个理由吗?你可以使用 preg_replace_callback() 吗?
  • 是的,实际上任何 preg 都是首选 =) 我正在尝试编写自己的解析器并尝试为上述示例找出正则表达式。
【解决方案2】:

您不需要regex 来执行此操作。将页面的元数据放在这样的数组中:

$meta["title"] = "Title";
$meta["description"] = "Description of the Page";
$meta["keywords"] = "Keywords, SEO";

以这种方式输出三个:

<title><?php echo $meta["title"]; ?></title>
<meta name="description" content="<?php echo $meta["description"]; ?>">
<meta name="keywords" content="<?php echo $meta["keywords"]; ?>">

【讨论】:

  • 再说一次,这行不通,我需要实际的 preg_replace + regex 来做这样的事情 {{title=some title here}} 所以我可以像上面的例子一样使用它。
  • 是的,所以我可以在我的页面中使用自定义元素。 Like = Heading 1 = and underline text 等。诀窍是允许常规 HTML/HTML5。但同时允许解析页面中我的自定义元素。
  • 我实际上已经从某个 php 类中找到了这个 > pastebin.com/wSarUysb,它与我需要做的事情类似,但我似乎无法将其扩展到 GeSHi 源代码突出显示的源代码标签。另外,我想改用我的自定义元素。
【解决方案3】:

匹配任何给定的标签:

/(?<=\{\{TAG_NAME=).*?(?=\}\})/

要匹配变量标签:

/\{\{(\w*?)=(.*?)\}\}/

然后,第一个子匹配会给你标签名称,第二个会给你值。考虑空格:

/\{\{\s*(\w*?)\s*=\s*(.*?)\s*\}\}/

...只要没有人在标签中使用'}}'。

崩溃:

\{\{

匹配两个开括号。简单的。 (它们必须被转义,因为 { 是正则表达式中的特殊字符。

\s*

尽可能多地匹配空白。

(\w*?)

匹配不会破坏正则表达式的最短单词字符串(a-zA-Z0-9 和下划线)。括号将此处匹配的内容作为子匹配返回。

\s*=\s*

用一个等号来吞噬更多的空白

(.*?)

匹配不会破坏正则表达式的任何字符的最短集合,并将其作为第二个子匹配返回。

\s*\}\}

吞噬最后一个空格和右大括号(再次,转义)。

所以,如果你这样做:

$regex = '/\{\{\s*(\w*?)\s*=\s*(.*?)\s*\}\}/'
preg_match_all($regex, $html, $matches)
$html = preg_replace($regex, '', $html)

那么$matches[1] 拥有你所有的标签名称,$matches[2] 拥有它们的所有值,$html 拥有你所有剩余的html

【讨论】:

    猜你喜欢
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 2015-03-07
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多