2015-05-06 2 views
0

Я пытаюсь получить копию истории.Анализ содержимого изнутри div, игнорирующего диапазон

$url = 'http://www.myfoxchicago.com/story/28987351/cook-county-jail-guards-sick-calls-tripled-on-night-of-big-fight'; 

//$content = file_get_content($url); 

$content = '<div id="WNStoryBody" class=""><span id="WNStoryDateline">CHICAGO (FOX 32 News) - 
      </span><p>The Mayweather-Pacquiao...</p></div>'; 

preg_match_all('<div id=\"WNStoryBody\" class=\"\"> 
    <span id=\"WNStoryDateline\">CHICAGO (FOX 32 News) - 
      <\/span>(.*?)<\/div>', $content, $matches); 

print_r($matches); 

Нужный выход:

<p>The Mayweather-Pacquiao boxing match was billed as the fight of the century, and it may have contributed to a massive number of correctional officers calling in sick at the Cook County Jail this past weekend.</p><p>Between 7:00 AM Saturday and 3:00 PM Sunday, 637 correctional officers called in sick out of the 3100 employees scheduled to work.</p><p>Sheriff Tom Dart is beyond frustrated.</p><p>“This was triple what the normal call in would be,” Dart said.</p><p>Between 20 and 25 percent of the staff on each of the four shifts in question took a sick day.</p><p>“We are not, didn't just fall off the truck, there was a big boxing match, a lot of people were talking about it, did that play into this?” Dart said. “We'd be naïve not to realize that certain big events seem to coincide with people not showing up for work at times here,” he said.</p><p>But Teamsters Local 700, which represents the correctional officers, believes those accusations are a low blow to guards who took a sick day that they needed.</p><p>“I don't believe that the officers called in to watch the fight,” said Dennis Andrews, Business Agent for Teamsters Local 700.</p><p>Andrews attributes the sick call to coincidence and stress.</p><p>“The stress level on the officers is off the charts. So I think this is a culmination of the building up for the past several weeks of all the inmate fights, the inmates attacking staff,” Andrews said.</p><p>Sheriff Dart said what happened this weekend was horribly unfair to good employees and taxpayers.</p><p>“We have pled with people over and over again, listen, there's all sorts of mechanisms here so that you can take care of family members, people who are sick, but you can't just call in sick because you want a day off,” Dart said.</p><p>The sheriff said it could be hard to hand down discipline directly as a result of the sick calls, but he does monitor sick day abuse and it could impact the ability of employees to be promoted.</p><p>The Teamsters don't believe anyone should face discipline.</p><p>“If an officer has sick time and he called in sick, that's his earned time to used, there can't be any discipline,” Andrews said. “I don't believe anybody used it as a vacation day.”</p> 

ответ

0

preg_match не сложный способ разбора HTML, можно использовать

$doc = new DOMDocument(); 
$doc->loadHTML($content); 

$div = $doc->getElementById('WNStoryBody'); 

$ptag = new DOMDocument(); 
$ptag->loadHTML($div); 

$all_ptags = $ptag->getElementsByTagName('p'); 
// you get array of <p> tags here 
// you can use implode if you want to convert it to a string or use foreach to use each <p> tag seprately 
+0

Я думаю, '$ all_ptags = $ dom-> getElementsByTagName ('р'), 'должно было быть:' $ all_ptags = $ ptag-> getElementsByTagName ('p'); ' – user3638589

+0

oops, это была ошибка! – Viral

+0

Благодарим вас за помощь! :) – user3638589

0

Использование DOMDocument для этого:

$dom = new DOMDocument; 
$dom->loadHTML($content); 
$elements = $dom->getElementById('WNStoryBody'); 
foreach($elements as $element) 
    print str_replace(Array("<span id=\"WNStoryDateline\">", "</span">), "", $element->nodeValue); 

Может быть, вы должны продлить str_replace или сделать его preg_replace.

Используйте ответ от Viral, это лучший метод, потому что вы ничего не должны заменять.

Смежные вопросы