简体   繁体   中英

Use php to read static html page, add meta tags, then load modified page

I am trying to use PHP to inject meta tags into an existing html file that contains several javascript commands. The overall issue I am trying to solve is to get the open graph meta tags for Facebook into the file that gets called on the application that is written in javascript.

Using PHP, I have tried reading in the index.html file, find the tag, insert the meta tags, and then echo the modified file. Problem is that the modified file always ends up showing the head tag as follows, skipping the tags I added in by putting them further down the file.

<head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>

The one javascript index.html file gets reused for several different pages, so I can't just put the completed tags in it since they could be different every time.

How can I inject the 6 different meta og and one fb tags after the <head> tag?

In your php file, specify a variable. could be anything, as an example:

$var=$randomarray[0][pagetitle];

insert it into the head output

<meta property="og:title" content="<?php echo"$var";?>" />

if your php file contains a body tag then it is easy just do:

$file = file_get_contents('index.html');    
$str = '<meta /></head>';    
$file = str_replace('</head>', $str, $file );
echo $file;

just replace $str with your expected html, be sure to add " </head> " at the end.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM