同じディレクトリ内の外部phpファイルからhtmlファイルに新しいメタタグを書き込もうとしています。jQuery を使用してメタ タグの存在をテストし、メタが存在しない場合は ajax を介して php 関数を呼び出します。したがって、後続のページの読み込みでは問題ありません。
これを行う関数を構築しようとしていますが、その 1 つの側面で少し助けが必要です。先頭の head タグを含む行がどのように記述されるかは保証できなかったので、これは何らかの形式の正規表現である必要があると思います。以下のコードを参照してください。
// So the line with the opening head tag may for arguments sake look like this one below :
// <!DOCTYPE html><html lang="en" data-cast-api-enabled="true"><head> <link id="css-680430062" rel="stylesheet" href="http://s.ytimg.com/yts/cssbin/www-core-vfl8iz0yT.css">
function insert_into_file($file_path, $text, $after = true) {
$contents = file_get_contents($file_path);
$insert_marker = ""; // this should be the line containing the opening head tag saved to the variable as a string
$new_contents = preg_replace($insert_marker, ($after) ? '$0' . $text : $text . '$0', $contents);
return file_put_contents($file_path, $new_contents);
}