php if - 它是如此内存密集吗?

时间:2011-09-14 14:48:31

标签: php memory-management

刚才我用preg_replace运行脚本,在大型数据行集上,我有这行代码:

$words = array("http", "www", "com", "org", "related", "cache", "asp", "php", "html", "wid", "roomid", "wmaster", "pcash", "sdk", "vippath");
foreach ($words as $seek):  
    if ($row['title'] = preg_replace("/(\S*)$seek(\S*)/i", "",$row['title'])):
    endif;
endforeach;

现在,当我运行超过1000行时,我会得到memory_limit错误,但是,当我将代码更改为:

$words = array("http", "www", "com", "org", "related", "cache", "asp", "php", "html", "wid", "roomid", "wmaster", "pcash", "sdk", "vippath");
foreach ($words as $seek):  
    $row['title'] = preg_replace("/(\S*)$seek(\S*)/i", "",$row['title']);
endforeach;

我能够在40,000行上运行它而没有达到内存限制......

有人对此有任何见解吗?

编辑:

if ($res = parent::dbDelta("SELECT id, title, used FROM " . databaseConn::TITLE_TABLE . " LIMIT 40000, 0", true)):
$query .= "INSERT INTO tmp (title, used) VALUES ";
while ($row = mysql_fetch_array($res)):
$fail = false;
    $words = array("http", "www", "com", "org", "related", "cache", "asp", "php", "html", "wid", "roomid", "wmaster", "pcash", "sdk", "vippath");
    foreach ($words as $seek):  
            $row['title'] = preg_replace("/(\S*)$seek(\S*)/i", "",$row['title']);
    endforeach;

    preg_match_all('/\d+/', $row['title'], $matches); 
    $numbers = $matches[0];
    $numbers = array_map('intval', $numbers);  
    foreach ($numbers as $num):
        if (($num > 100) &&  ($num != 365) && ($num != 247) && ($num != 360) && ($num != 1800) && ($num != 800) && ($num != 1900) && ($num != 900)):
            $fail = true;
            $row['title'] = str_replace("$num", "",$row['title']);
        endif;
    endforeach;

    if ((str_word_count($row['title'])) < 3):
        $fail = true;
    endif;

    if (!$fail):
        $row['title'] = trim($row['title']);
        $query .= "('$row[title]', '$row[used]'), ";
    endif;


$i++;
endwhile;
        $query = substr(trim($query), 0, -1);
        $query .= " ON DUPLICATE KEY UPDATE used='0'";
        mysql_query($query) or die(mysql_error());
endif;
}

2 个答案:

答案 0 :(得分:1)

尝试以下方法:

$words = array("http", "www", "com", "org", "related", "cache", "asp", "php", "html", "wid", "roomid", "wmaster", "pcash", "sdk", "vippath");
$regex = "/(\S*)".implode('|',$words)."(\S*)/i";
$row['title'] = preg_replace($regex,"",$row['title']);

答案 1 :(得分:0)

我认为你的if语句的问题在于它设置变量而不是比较它。