如何使用PHP

时间:2015-11-22 22:27:01

标签: php html web backend

好的,所以我很失落和沮丧,我一直在阅读和工作几个小时,当我觉得我取得了进展......我发现我真的没有。

问题:从像cnn.com这样的地方收集一篇新闻文章,并制作一个PHP网络应用程序,用于标出标题,故事和任何照片。确保删除所有HTML标记。 必须使用类,构造函数,并且不能使用第三方库,本机PHP是可以的。

所以这是我正在撰写的文章 http://www.cnn.com/2015/11/18/us/delmarva-peninsula-squirrel-endangered-species-feat/index.html

我最后因挫折而离开的地方就是这样,它会提取我需要的所有内容,但也会显示原始代码。

$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>");

我一度也有这个

//Class Structure
class ThisIsMyClass {
    public $aHTMLContent = 'HTMLContent';
    public $aHeader = 'Header';
    public $aPictures = 'Pictures';
}

$ThisIsMyClass = new ThisIsMyClass;


//Title For Page
$element = 'Header'
$ThisIsMyClass->$element=file_get_contents("http://www.cnn.com/tech", 20);
preg_match("/<title>(.*)</title>/i", $html, $match);
$title = $match[1];
echo $element . '<br>';

//HTML Content
$element2 = 'HTMLContent'
$ThisIsMyClass->$element2=getHTML("http://www.cnn.com/tech");
echo $element2 . '<br>';

//Get all images from page
$element3 = 'Pictures';
foreach($element2->find"img" as $element3)
echo $element3->src . '<br>';

?>

我看到了大量的“使用卷曲”,但由于卷曲是第三方,我无法使用它。我已经看到了特定的标题,但实际的标题是元标记。我在这里用了几个例子来提取元标记,但我得到的错误显然没有其他帖子得到。

请帮忙!今晚我需要完成这项工作,我已经完成了我的尽职调查,我只是不知道还有什么要去看。

好的......所以我花了最后两个小时试着看看如何在有意义的地方添加一个类和构造函数....

我能找到的每个例子都是陈规定型的

class human
{
function talk{
echo "Hello"}
}
等等......我很难看出这与我的情况有什么关系。我基本上没有Chris85在下面发布的代码结构。我没有看到下面的代码中的信息如何涉及类和构造函数....有人可以帮助提供一些提示,示例或一个好的教程,其中涉及的内容更接近我正在使用的内容吗?

1 个答案:

答案 0 :(得分:0)

以下是使用DOM解析器输出所有h1imgp元素内容的粗略示例。

$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.