I'm trying to figure out a way to create description and keywords per page.
For the title it would be:
{{title=some page title in here}}
For descriptions, I would do something like this:
{{description=some description per page in here}}
Also for keywords meta tag, I would do something like this:
{{keywords=example keyword, per each page, this is an example}}
How would I achive this with preg_replace + regex parsing, also so it would not be visible on the page it self but placed in the actual meta information such as:
<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">
Example page would look like this:
{{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-->
and of course result would be simular to this:
<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>
Thank you so much for the help.
If I'm reading this right, you want to include something like this:
<title><?php echo $page_title; ?></title>
Where page title has been set earlier in the script
You don't need regex
to do this. Have the metadata of the page in an array like this:
$meta["title"] = "Title";
$meta["description"] = "Description of the Page";
$meta["keywords"] = "Keywords, SEO";
Output the three this way:
<title><?php echo $meta["title"]; ?></title>
<meta name="description" content="<?php echo $meta["description"]; ?>">
<meta name="keywords" content="<?php echo $meta["keywords"]; ?>">
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.