如何使用PHP从新闻文章中吐出标题、照片和故事

如何使用PHP从新闻文章中吐出标题、照片和故事,php,html,web,backend,Php,Html,Web,Backend,好吧,所以我相当失落和沮丧,我已经阅读和工作了几个小时,当我觉得我取得了进展…我发现我真的没有 问题:从cnn.com这样的网站上获取一篇新闻文章,制作一个php web应用程序,它可以显示标题、故事和任何照片。确保去掉所有HTML标记。 必须使用类、构造函数,不能使用第三方库,原生PHP可以 这是我正在写的文章 我最后离开的地方是,它提取了我需要的所有东西,但也留下了原始代码 $url='http://www.cnn.com/2015/11/18/us/delmarva-peninsula-

好吧,所以我相当失落和沮丧,我已经阅读和工作了几个小时,当我觉得我取得了进展…我发现我真的没有

问题:从cnn.com这样的网站上获取一篇新闻文章,制作一个php web应用程序,它可以显示标题、故事和任何照片。确保去掉所有HTML标记。 必须使用类、构造函数,不能使用第三方库,原生PHP可以

这是我正在写的文章

我最后离开的地方是,它提取了我需要的所有东西,但也留下了原始代码

$url='http://www.cnn.com/2015/11/18/us/delmarva-peninsula-squirrel-endangered-species-feat/index.html';

$element = file_get_contents($url);
echo strip_tags($element , "<h1>, <img>, <p>");

等等……我很难看出这与我所做的有什么关系。我基本上脱离了Chris85在下面发布的代码结构。我看不出下面代码中的信息是如何涉及类和构造函数的……有人能提供一些提示、示例或一个好的教程来帮助我吗?这是一个使用DOM解析器输出所有
h1
img
,的粗略示例,和
p
元素含量

class human
{
function talk{
echo "Hello"}
}
输出:

$url='http://www.cnn.com/2015/11/18/us/delmarva-peninsula-squirrel-endangered-species-feat/index.html';
$string = file_get_contents($url);
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($string);
libxml_use_internal_errors(false);
output_these_elements(array('h1', 'img', 'p'), $doc);
function output_these_elements($elements, $doc) {
    foreach($elements as $element) {
        echo $element . "\n\n";
        $domelements = $doc->getElementsByTagName($element);
        foreach($domelements as $domelement) {
            if($element == 'img') {
                echo $domelement->getAttribute('src') . "\n";
            } else {
                echo $domelement->nodeValue . "\n";
            }
        }
    }
}

您意识到
$element='Header'
$element2='HTMLContent'
都缺少结束分号。如果这是您的实际代码,那么错误报告将抛出解析错误。对于HTML,您可能会发现使用解析器比使用正则表达式更好。PHP中也内置了本机解析器,我试过查看DOMDocument和简单的DOM解析器,我看到的每个示例都以数组的形式将它们放入输出中。或者他们用它来吐出一个循环,比如一个评论提要,所以需要一个循环。我找不到适用于我的应用程序的示例。谢谢,这实际上非常有用,我认为我看到的其他示例的输出让我感到困惑。这给了我一点继续向前看如何显示图像,并可能摆脱“h1”、“img”和“p”文本的输出
$url='http://www.cnn.com/2015/11/18/us/delmarva-peninsula-squirrel-endangered-species-feat/index.html';
$string = file_get_contents($url);
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($string);
libxml_use_internal_errors(false);
output_these_elements(array('h1', 'img', 'p'), $doc);
function output_these_elements($elements, $doc) {
    foreach($elements as $element) {
        echo $element . "\n\n";
        $domelements = $doc->getElementsByTagName($element);
        foreach($domelements as $domelement) {
            if($element == 'img') {
                echo $domelement->getAttribute('src') . "\n";
            } else {
                echo $domelement->nodeValue . "\n";
            }
        }
    }
}
h1

Nuts to you! Squirrel gets off endangered species list
img

data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
http://i2.cdn.turner.com/cnnnext/dam/assets/151118110249-01-animals-endangered-species-super-169.jpg
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
http://i2.cdn.turner.com/cnnnext/dam/assets/151118110527-02-animals-endangered-species-restricted-super-169.jpg
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
http://i2.cdn.turner.com/cnnnext/dam/assets/151118110554-03-animals-endangered-species-super-169.jpg
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
http://i2.cdn.turner.com/cnnnext/dam/assets/151118110627-04-animals-endangered-species-super-169.jpg
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
http://i2.cdn.turner.com/cnnnext/dam/assets/151118110842-05-animals-endangered-species-super-169.jpg
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
http://i2.cdn.turner.com/cnnnext/dam/assets/151118110903-06-animals-endangered-species-super-169.jpg
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
http://i2.cdn.turner.com/cnnnext/dam/assets/151118110938-07-animals-endangered-species-super-169.jpg
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
http://i2.cdn.turner.com/cnnnext/dam/assets/151118111005-08-animals-endangered-species-super-169.jpg
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
http://i2.cdn.turner.com/cnnnext/dam/assets/151118111029-09-animals-endangered-species-super-169.jpg
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
http://i2.cdn.turner.com/cnnnext/dam/assets/151118111059-10-animals-endangered-species-super-169.jpg
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
http://i2.cdn.turner.com/cnnnext/dam/assets/151118111206-11-animals-endangered-species-super-169.jpg
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
http://i2.cdn.turner.com/cnnnext/dam/assets/151118111225-12-animals-endangered-species-super-169.jpg
data:image/gif;base64,R0lGODlhEAAJAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAAAQAAkAAAIKlI+py+0Po5yUFQA7
http://i2.cdn.turner.com/cnnnext/dam/assets/151118111242-13-animals-endangered-species-super-169.jpg
http://i2.cdn.turner.com/cnnnext/dam/assets/151118110249-01-animals-endangered-species-small-11.jpg
http://i2.cdn.turner.com/cnnnext/dam/assets/151118110527-02-animals-endangered-species-restricted-small-11.jpg
http://i2.cdn.turner.com/cnnnext/dam/assets/151118110554-03-animals-endangered-species-small-11.jpg
http://i2.cdn.turner.com/cnnnext/dam/assets/151118110627-04-animals-endangered-species-small-11.jpg
http://i2.cdn.turner.com/cnnnext/dam/assets/151118110842-05-animals-endangered-species-small-11.jpg
http://i2.cdn.turner.com/cnnnext/dam/assets/151118110903-06-animals-endangered-species-small-11.jpg
http://i2.cdn.turner.com/cnnnext/dam/assets/151118110938-07-animals-endangered-species-small-11.jpg
http://i2.cdn.turner.com/cnnnext/dam/assets/151118111005-08-animals-endangered-species-small-11.jpg
http://i2.cdn.turner.com/cnnnext/dam/assets/151118111029-09-animals-endangered-species-small-11.jpg
http://i2.cdn.turner.com/cnnnext/dam/assets/151118111059-10-animals-endangered-species-small-11.jpg
http://i2.cdn.turner.com/cnnnext/dam/assets/151118111206-11-animals-endangered-species-small-11.jpg
http://i2.cdn.turner.com/cnnnext/dam/assets/151118111225-12-animals-endangered-species-small-11.jpg
http://i2.cdn.turner.com/cnnnext/dam/assets/151118111242-13-animals-endangered-species-small-11.jpg
p

By Todd Leopold, CNN

Updated 3:54 PM ET, Wed November 18, 2015 
 (CNN)Forget about gathering nuts. A rare species of squirrel has gathered new life.
The Delmarva Peninsula fox squirrel, one of the animals on the original endangered species list, is now no longer at risk of extinction, says the U.S. Department of the Interior.
"The fox squirrel's return to this area, rich with farmland and forest, marks not only a major win for conservationists and landowners, but also represents the latest in a string of success stories that demonstrate the Endangered Species Act's effectiveness," Interior official Michael Bean said from Prime Hook National Wildlife Refuge in Milton, Delaware, according to a Department of the Interior statement. 
The squirrel -- native to Delaware and coastal Maryland and Virginia (DelMarVa, get it?) -- will be officially removed from the list in December.
The Delmarva Peninsula fox squirrel was one of 78 species listed under the Endangered Species Preservation Act in 1967, the first year the list appeared. It continued on the successor list of Threatened and Endangered Wildlife under the Endangered Species Act, passed in 1973.
It's one of 30 species to have been delisted, according to the Interior Department. Others include the peregrine falcon, the American alligator and the national bird of the United States, the bald eagle.
Not all species make it, despite being listed. The Santa Barbara song sparrow was noted as extinct in 1983 despite 10 years on the list. Also considered extinct are the longjaw cisco, a fish once found in the Great Lakes; the Mariana mallard, a duck native to the Mariana Islands; and the Caribbean monk seal, a once-common Gulf of Mexico seal -- Columbus recorded their existence in 1494 -- that was delisted in 2008 after an exhaustive search.
The Delmarva Peninsula fox squirrel also had a difficult climb back to its current population. Larger than your average suburban nut-hoarder, it was widely found in the rural areas of the Delmarva Peninsula, a landmass between the Chesapeake Bay and the Atlantic Ocean. 
But decades of development and overhunting, starting in the 1950s, limited the fox squirrel's habitat and threatened its existence.
However, "since listing, the squirrel's range has increased from four to 10 counties, and a population of up to 20,000 squirrels now covers 28 percent of the Delmarva Peninsula, primarily in Maryland," the Interior Department's statement observed. (No word on whether Atlanta's Inman Park Squirrel Census contributed to the counting.)
Still, just because the squirrel is no longer endangered, don't expect to head out immediately and start grabbing a few for dinner. According to a fact sheet, "Deliberate killing of any animal is permissible only during an open hunting season defined under state law in Maryland, Delaware and Virginia." Those states haven't decided on any details.
Not bad, Delmarva Peninsula fox squirrel. You're out of the woods -- that is, if you wanted to leave.