开发者

Parsing HTML tags with Smarty

Is there I simple way to get contents of a <h1> tag from a html file, using PHP (or Smarty), on the server side?

Background is that I need to do some SEO on a legacy Smarty based PHP site, with bunch of content pages with no head section defined.

I.e., the main index.php page loads a shared index.tpl template which contains the <head> section along with <title> and meta description tags for all pages, and then just grabs the content from individual pXXX.html, based on an id parameter in the url. Each "content" page has an <h1> tag where the relevant title is stored, but the page title remains fixed all the time.

Since there is too many 开发者_Python百科of them to edit each of them individually, I wanted to see if I could get their each by parsing the page on request. I guess I could make a script which would do some batch processing, but I am looking for a quick solution which wouldn't modify these content pages.

I am open to other suggestions, also. To be honest, I never used Smarty before, so I might be missing something here.


preg_match('~<h1>(.+)</h1>~', file_get_contents('original_file.html'), $result);
var_dump($result[1]);


Well, in index.php you could :

  • get content of pXXX.html in a var (before loading index.tpl)
  • create a smarty var, for example $content
  • parse content to get h1 title
  • create a smarty var, for example $title
  • modify index.tpl to display $title and $content
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜