PHP-获取两个元素的内部HTML代码
我目前在一个过渡过程中,我想使我现有的网站CMS。直到现在(几年前),我一直在生成和保存完整的html文件,我想将这些页面的内容存储在数据库中。我想,我的运气是,我想从每个html中提取的两个元素在一个html文件中是唯一的,在所有文件中都是相同的。我试过这个:PHP-获取两个元素的内部HTML代码,php,innerhtml,Php,Innerhtml,我目前在一个过渡过程中,我想使我现有的网站CMS。直到现在(几年前),我一直在生成和保存完整的html文件,我想将这些页面的内容存储在数据库中。我想,我的运气是,我想从每个html中提取的两个元素在一个html文件中是唯一的,在所有文件中都是相同的。我试过这个: if ($handle = opendir('.')) { while (false !== ($entry = readdir($handle))) { if ($entry != "." &&
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);
}
if($handle=opendir('.')){
while(false!==($entry=readdir($handle))){
如果($entry!=“&&&$entry!=”){
$string=file\u get\u contents($entry);
$pattern=“/(**?/”;
preg_match_all($pattern、$string、$uname);
$pattern='/(.*?/);
preg_match_all($pattern、$string、$udesc);
echo“名称:”.$uname[1][0]。“
”;
echo“DESC:.$udesc[1][0]。”
”;
//MYSQL将在这里保存
}
}
closedir($handle);
}
上面的代码提取(h1)名称(/h1)(假设(=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);
}
if($handle=opendir('.')){
while(false!==($entry=readdir($handle))){
如果($entry!=“&&&$entry!=”){
$string=file\u get\u contents($entry);
$doc=新的DOMDocument();
$doc->loadHTML($string);
$h1=$doc->getElementsByTagName('h1')->item(0)->textContent;
回声“名称:”.$h1.“
”;
$p=$doc->saveHtml($doc->getElementsByTagName('p')->项目(0));//$p=$doc->getElementsByTagName('p')->项目(0)->文本内容;加载内容,但没有html标记,因此我无法使用它…:S
回声“描述:.$p.”
”;
//MYSQL将在这里保存
}
}
closedir($handle);
}
上面的代码可以工作,但并不像预期的那样。我需要完整的HTML代码的段落,而不仅仅是文本。我也试过$doc->savehtml(),但还是没有
请帮忙,提前谢谢 删除
->textContent
$h1 = $doc->saveHtml($doc->getElementsByTagName('h1')->item(0));
echo "NAME: ".$h1."<br>";
$p = $doc->saveHtml($doc->getElementsByTagName('p')->item(0));
$h1=$doc->saveHtml($doc->getElementsByTagName('h1')->项(0));
回声“名称:”.$h1.“
”;
$p=$doc->saveHtml($doc->getElementsByTagName('p')->item(0));
您是否尝试过$doc->saveHtml($doc->getElementsByTagName('p')->项(0))
(没有->textContent
)?@metadings:是的,现在再试一次以确定。不走运(