简体   繁体   English

如何使用“PHP Simple HTML DOM Parser”获取内容 <h1></h1> 标签?

[英]How can I use “PHP Simple HTML DOM Parser” to get the contents of an <h1></h1> tag?

I'm new to PHP =) Right now I am using PHP includes for my site template. 我是PHP的新手=)现在我正在使用PHP包含我的网站模板。 I have my header, containing all my <head></head> info. 我有我的标题,包含我的所有<head></head>信息。 What I want to do is write a code that will take the contents of the <h1></h1> tag from the page, and echo it into the <title></title> tag in my header.php include. 我想要做的是编写一个代码,该代码将从页面中获取<h1></h1>标记的内容,并将其回显到我的header.php include中的<title></title>标记。

I got the PHP Simple HTML DOM Parser from here: [ http://simplehtmldom.sourceforge.net/][1] , and I found a code (I forget where in all my googling) that goes like this: 我从这里得到了PHP Simple HTML DOM Parser:[ http://simplehtmldom.sourceforge.net/] [1 ] ,我发现了一个代码(我忘记了所有谷歌搜索中的内容),如下所示:

<?php
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$html = file_get_html('http://www.myurl.com/');
foreach($html->find('#content h1') as $element){
    echo $element->plaintext;}
?>

That I think is supposed to echo the h1 tag contents? 我认为应该回应h1标签内容? Like I said, I'm new to PHP and I only know the basics, and I don't know really know any OOP (yet), so I'm sorry if I'm asking a dumb question. 就像我说的,我是PHP新手,我只知道基础知识,我不知道真的知道任何OOP(还),所以如果我问一个愚蠢的问题,我很抱歉。

It looks like it's getting the current page, then putting the contents of the h1 tag into the variable $element, and then echoing it. 看起来它正在获取当前页面,然后将h1标记的内容放入变量$ element,然后回显它。 But nothing happens when I put it into my page. 但是当我把它放到我的页面时没有任何反应。 Can anyone help me with what I'm doing wrong? 任何人都能帮我解决我做错的事吗? Thank you for reading!! 谢谢你的阅读!! =) =)

EDIT: Here's my HTML 编辑:这是我的HTML

From the header.php file: 从header.php文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<?php
/* current page url */
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
?>

<?php include '/home/dreami14/public_html/simplehtmldom/simplehtmldom/simple_html_dom.php' ?>

<title>
<?php
$url = curPageURL();
$html = file_get_html($url);
foreach($html->find('#main h1') as $element){
    echo $element->plaintext;}
?></title>

<link rel="stylesheet" type="text/css" href="/stylesheet.css" />
</head>
<body>

From test.php: 来自test.php:

<?php include '/home/dreami14/public_html/design/includes/head.php' ?>

<div id="main">
<h1>This should be the title</h1>
<p>Blah blah</p>
</div>

</body>
</html>

I don't get any errors, but my <title></title> is empty. 我没有收到任何错误,但我的<title></title>是空的。

Edit to add: also, I echoed $url in the document itself so I know that part is working 编辑添加:另外,我在文档中回显了$ url,所以我知道该部分正在运行

You're not saying how your HTML is structured, but if you want to find the h1 with the ID content you need to use 您没有说明HTML的结构,但是如果您想要找到具有您需要使用的ID contenth1

foreach($html->find('h1#content') as $element){

the way you are doing it right now, it says "find any h1 element within another element with the ID content ". 你现在正在做的方式,它说:“找到任何h1与ID的另一个元素 content ”。

I would restructure your code a little. 我会稍微重构你的代码。 Basically, you are trying to get the content in h1 before it is populated. 基本上,您在填充之前尝试获取h1的内容。 In your test.php I would define an array with meta data and then include the header. test.php我将使用元数据定义一个数组,然后包含标题。
Like so: 像这样:

test.php

<?php 
$meta = array();
$meta['title'] = "This should be the title";

include '/home/dreami14/public_html/design/includes/head.php' 

?>

<div id="main">
<h1><?php echo $meta['title'] ?></h1>
<p>Blah blah</p>
</div>

</body>
</html>

head.php : head.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   <title>
 <?php echo (isset($meta) && isset($meta['title'])) ? $meta['title'] : "Default title"; ?>
   </title>
   <link rel="stylesheet" type="text/css" href="/stylesheet.css" />
</head>
<body>

But if you start to do more complicated stuff, you should have a look at the Model-View-Controller design pattern and eg the Zend framework , which implements it. 但是如果你开始做更复杂的事情,你应该看看模型 - 视图 - 控制器设计模式,例如实现它的Zend框架

I thinks it's the way, only print child content: 我认为这是方式,只打印儿童内容:

html = file_get_html($url);
foreach($ret->children as $child) {  
   echo $child;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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