我目前正處於一個轉換過程中,我想要製作我現有的網站的CMS。直到現在(幾年),我正在生成並保存完整的html文件,並且我想將這些頁面的內容存儲在數據庫中。我認爲,我的運氣是我想從每個html獲取的兩個元素在一個html文件中是唯一的,而在所有文件中都是相同的。我曾經嘗試這樣做:PHP - 獲取兩個元素的內部HTML代碼
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
$string= file_get_contents($entry);
$pattern = "/<h1>(.*?)<\/h1>/";
preg_match_all($pattern, $string, $uname);
$pattern = '/<p class=\"user_info\"><strong>(.*?)<\/strong><\/p>/';
preg_match_all($pattern, $string, $udesc);
echo "NAME: ".$uname[1][0]."<br>";
echo "DESC: ".$udesc[1][0]."<br>";
//MYSQL SAVING WILL GO HERE
}
}
closedir($handle);
}
以上代碼提取物(H1)名稱(/ H1)(想象(== <和)==>)的一部分,但不是(P CLASS = 「USER_INFO」)(強)內容(/ strong)(/ p)部分,它只是空白。
我也嘗試了不同的方法:
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
$string= file_get_contents($entry);
$doc = new DOMDocument();
$doc->loadHTML($string);
$h1 = $doc->getElementsByTagName('h1')->item(0)->textContent;
echo "NAME: ".$h1."<br>";
$p = $doc->saveHtml($doc->getElementsByTagName('p')->item(0)); // $p = $doc->getElementsByTagName('p')->item(0)->textContent; loads content, just without html tags, so I can not use it... :S
echo "DESC: ".$p."<br>";
//MYSQL SAVING WILL GO HERE
}
}
closedir($handle);
}
上面的代碼工作,但並不如預期。我需要完整的段落HTML代碼,而不僅僅是文本。我也嘗試了$ doc-> savehtml(),仍然沒有。
請幫忙,並提前致謝!
你嘗試'$ doc-> saveHtml($ doc->的getElementsByTagName( 'P') - >項目(0));'(不' - > textContent')? – metadings
@metadings:是的,現在再試一次就可以了。沒有運氣。 :( – DekiB