![](/img/trans.png)
[英]How to transfer a variable that contains a java or python code from php file to an empty file (.java or .py)?
[英]How to transfer code from and create .html file into a .php file
我想創建一個PHP程序,該程序可以創建一個可以讀取此HTML文件的.php文件:
<html>
<head>
<title> Hello World! </title>
</head>
<body>
</body>
</html>
它將搜索<head>
和</head>
標記(包括中間的標記),並將選定的標記傳輸到header.php。
因此,header.php將具有以下內容:
<head>
<title> Hello World! </title>
</head>
另一個問題是,如何在頁面的開頭插入PHP標記< ?php
,並在PHP文件的最后插入?>
。
我已經在這里完成了此代碼,但尚未完成,並且我沒有讀取<head>
標記。
<?php
firstIdentifier = '<head>';
$secondIdentifier = '</head>';
$currentContent = str_replace("\n", "", file_get_contents('sourcefile.txt'));
$pattern = '/('.$firstIdentifier.')(.+?)('.$secondIdentifier.')/';
//get all text between the two identifiers, and include the identifiers in the match result
preg_match_all($pattern, $currentContent , $matches);
//stick them together with space delimiter
$contentOfNewFile = implode(" ",$matches[0]);
//save to a new file
$newFile = fopen('destinationFile.txt','a');
fwrite($newFile, $contentOfNewFile);
?>
請幫忙...
我認為您正在這樣做。 您是否考慮過使用SimpleXML庫將HTML解析為XML? 你可以那樣做! 請允許我解釋一下:
假設您將文件讀入一個名為$ html_main的字符串變量,現在考慮以下代碼:
$dom = new DOMDocument("1.0", "UTF-8");
$dom->loadHTML($html_main);
$xml = simplexml_import_dom($dom);
使用XPATH(// html / head)將為您提供節點,只需要調用函數即可:
$xpathObj = $xml->xpath('//html/head');
然后,您可以將HTML代碼作為集合進行管理! 我認為,如果您打算進行一些主要的分析,那么這可能是一個更好的方法!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.