0

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

对于标题,它将是:

{{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>

非常感谢你的帮助。

4

3 回答 3

0

To match any given tag:

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

To match variable tags:

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

Then, the first submatch will give you the tag name, the second will give you the the value. To account for whitespace:

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

... so long as noone uses a '}}' within a tag.

A break down:

\{\{

Match two open braces. Easy. (they have to be escaped because the { is a special character in regex.

\s*

Greedily match as much white space as you can.

(\w*?)

match the shortest string of word characters(a-zA-Z0-9, and underscore) that won't break the regex. The parenthesis return the stuff matched here as a sub match.

\s*=\s*

Gobble up more whitespace with exactly one equals sign

(.*?)

Match the shortest set of any characters that won't break the regex, and return it as the second sub match.

\s*\}\}

Gobble up the last of the white space and the closing braces (again, escaped).

So, if you do:

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

Then $matches[1] has all your tag names, and $matches[2] has all their values, and $html has all your remaining html

于 2012-08-31T01:25:40.100 回答
0

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

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

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

于 2012-08-31T00:54:45.777 回答
0

你不需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"]; ?>">
于 2012-08-31T00:58:06.760 回答