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

私がこの権利を読んでいるなら、あなたはこのようなものを含めたいと思うでしょう:

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

スクリプトの前半でページタイトルが設定されている場所

于 2012-08-31T00:54:45.777 に答える
0

任意のタグに一致させるには:

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

変数タグを照合するには:

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

次に、最初のサブマッチでタグ名がわかり、2番目のサブマッチで値がわかります。空白を説明するには:

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

...タグ内で「}}」を使用する人がいない限り。

内訳:

\{\{

2つの中括弧を一致させます。簡単。({は正規表現の特殊文字であるため、エスケープする必要があります。

\s*

できるだけ多くの空白を貪欲に一致させます。

(\w*?)

正規表現を壊さない単語文字の最短の文字列(a-zA-Z0-9、およびアンダースコア)に一致します。括弧は、ここで一致したものをサブ一致として返します。

\s*=\s*

ちょうど1つの等号でより多くの空白をむさぼり食う

(.*?)

正規表現を壊さない文字の最短セットと一致し、2番目のサブ一致として返します。

\s*\}\}

最後の空白と閉じ中かっこを飲み込みます(ここでもエスケープします)。

だから、あなたがそうするなら:

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

次に$matches[1]、すべてのタグ名、$matches[2]すべての値、および$html残りのすべてのhtmlがあります

于 2012-08-31T01:25:40.100 に答える
0

これを行う必要はありませんregex。ページのメタデータを次のような配列にします。

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

次のように3つを出力します。

<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 に答える