我必须在本课程的某个地方有内存泄漏或只是在我的服务器上占用内存的东西。例如,如果我file_get_contents(http://www.theknot.com)它将无法连接到服务器,因为它没有关闭,或者mysql关闭了连接,或者在极端情况下完成了将服务器连接掉一段时间我们可以甚至没有得到ping。我知道它在preg_match_all if block中的某个地方,但是我不知道什么会逃避我只能假设在正则表达式匹配上的大量处理,因为从远程站点获取的内容中的任何内容。有什么想法吗?
<?php
class Utils_Linkpreview extends Zend_Db_table
{
public function getPreviews($url) {
$link = $url;
$width = 200;
$height = 200;
$regex = '/<img[^\/]+src="([^"]+\.(jpe?g|gif|png))/';
/// $regex = '/<img[^\/]+src="([^"]+)/';
$thumbs = false;
try {
$data = file_get_contents($link);
} catch (Exception $e) {
print "Caught exception when attempting to find images: ". $e->getMessage(). "\n";
}
if (($data) && preg_match_all($regex, $data, $m, PREG_PATTERN_ORDER)) {
if (isset($m[1]) && is_array($m[1])) {
$thumbs = array();
foreach (array_unique($m[1]) as $url) {
if (
($url = $this->rel2abs($url, $link)) &&
($i = @getimagesize($url)) &&
$i[0] >= ($width-10) &&
$i[1] >= ($height-10)
) {
$thumbs[] = $url;
}
}
}
}
return $thumbs;
}
private function rel2abs($url, $host) {
if (substr($url, 0, 4) == 'http') {
return $url;
} else {
$hparts = explode('/', $host);
if ($url[0] == '/') {
return implode('/', array_slice($hparts, 0, 3)) . $url;
} else if ($url[0] != '.') {
array_pop($hparts);
return implode('/', $hparts) . '/' . $url;
}
}
}
}
?>
编辑 - Amal Murali的评论指出我使用PHP的DomDocument更好的方向。谢谢你!
结果如下:
public function getPreviews($url) {
$link = $url;
$thumbs = false;
try {
$html = file_get_contents($link);
} catch (Exception $e) {
print "Caught exception when attempting to find images: ". $e->getMessage(). "\n";
}
$dom = new DOMDocument();
@$dom->loadHTML($html);
$x = new DOMXPath($dom);
foreach($x->query("//img[@width > 200 or substring-before(@width, 'px') > 200 or @height > 200 or substring-before(@height, 'px') > 200]") as $node)
{
$url = $node->getAttribute("src");
$thumbs[] = $this->rel2abs($url, $link);
}
return $thumbs;
}
答案 0 :(得分:0)
编辑 - Amal Murali的评论指出我使用PHP的DomDocument更好的方向。谢谢你!
结果如下:
公共函数getPreviews($ url){ $ link = $ url; $ thumbs = false;
try {
$html = file_get_contents($link);
} catch (Exception $e) {
print "Caught exception when attempting to find images: ". $e->getMessage(). "\n";
}
$dom = new DOMDocument();
@$dom->loadHTML($html);
$x = new DOMXPath($dom);
foreach($x->query("//img[@width > 200 or substring-before(@width, 'px') > 200 or @height > 200 or substring-before(@height, 'px') > 200]") as $node)
{
$url = $node->getAttribute("src");
$thumbs[] = $this->rel2abs($url, $link);
}
return $thumbs;
}