无法重新声明read_file_docx()

时间:2016-01-12 10:54:38

标签: php

$searchfor = $_REQUEST["search"];


$connect = mysqli_connect("localhost", "root", "", "portal");
$cv = mysqli_query($connect, "select * from uploaded_cv");
$cvcount = mysqli_num_rows($cv);
for ($i = 1; $i <= $cvcount; $i++) {

    $cvrow = mysqli_fetch_array($cv);
    $filename = $cvrow["candidatecv"];
    echo $filename;


    function read_file_docx($filename) {

        $striped_content = '';
        $content = '';

        if (!$filename || !file_exists($filename))
            return false;

        $zip = zip_open($filename);

        if (!$zip || is_numeric($zip))
            return false;

        while ($zip_entry = zip_read($zip)) {

            if (zip_entry_open($zip, $zip_entry) == FALSE)
                continue;

            if (zip_entry_name($zip_entry) != "word/document.xml")
                continue;

            $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));

            zip_entry_close($zip_entry);
        }// end while

        zip_close($zip);

        //echo $content;
        //echo "<hr>";
        //file_put_contents('1.xml', $content);

        $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
        $content = str_replace('</w:r></w:p>', "\r\n", $content);
        $striped_content = strip_tags($content);

        return $striped_content;
    }

    $file = "resume/$filename"; // or /var/www/html/file.docx

    $content = read_file_docx($file);
    if ($content !== false) {

        $pattern = preg_quote($searchfor, '/');
        $pattern = "/^.*$pattern.*\$/m";


        if (preg_match($pattern . "i", $content, $matches)) {

            echo '<br>';
            echo "Found matches:\n";
            echo implode("\n", $matches);
        } else {
            echo "No matches found";
        }
    } else {
        echo 'Couldn\'t the file. Please check that file.';
    }}

这是要从文档文件中搜索关键字。但是当我运行它时,第一次循环工作然后下次显示重新声明函数的致命错误。我无法理解这一点。所以请帮助我。

1 个答案:

答案 0 :(得分:0)

<?php
    $searchfor = $_REQUEST["search"];

    /* Declare the function outside the loop */

    function read_file_docx($filename) {
        $stripped_content = '';
        $content = '';

        if (!$filename || !file_exists($filename)) return false;
        $zip = zip_open($filename);
        if (!$zip || is_numeric($zip)) return false;

        while ($zip_entry = zip_read($zip)) {
            if (zip_entry_open($zip, $zip_entry) == FALSE)  continue;
            if (zip_entry_name($zip_entry) != "word/document.xml") continue;
            $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
            zip_entry_close($zip_entry);
        }

        zip_close($zip);

        $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
        $content = str_replace('</w:r></w:p>', "\r\n", $content);
        $stripped_content = strip_tags($content);
        return $stripped_content;
    }


    /* query db */
    $connect = mysqli_connect("localhost", "root", "", "portal");
    $cv = mysqli_query($connect, "select * from uploaded_cv");
    $cvcount = mysqli_num_rows($cv);
    for( $i = 1; $i <= $cvcount; $i++ ) {

        $cvrow = mysqli_fetch_array($cv);
        $filename = $cvrow["candidatecv"];
        echo $filename;

        $file = "resume/$filename"; // or /var/www/html/file.docx

        /* call your function inside the loop */

        $content = read_file_docx($file);
        if ($content !== false) {
            $pattern = preg_quote($searchfor, '/');
            $pattern = "/^.*$pattern.*\$/m";

            if (preg_match($pattern . "i", $content, $matches)) {
                echo '<br>';
                echo "Found matches:\n";
                echo implode("\n", $matches);
            } else {
                echo "No matches found";
            }
        } else {
            echo 'Couldn\'t the file. Please check that file.';
        }
    }
?>