PHP面试题(1)

  • 第一题

写一个php脚本,抓取页面http://php.net/manual/en/langref.php右侧的目录列表.运行此脚本期望得到如下输出:

    [2016-12-17 13:44:30] fetching http://php.net/manual/en/langref.php
    [2016-12-17 13:44:31] parsing start
    [2016-12-17 13:44:32] the right side list is:
    PHP Manual (http://php.net/manual/en/index.php)
    Copyright (http://php.net/manual/en/copyright.php)
    PHP Manual (http://php.net/manual/en/manual.php)
    Getting Started (http://php.net/manual/en/getting-started.php)
    Installation and Configuration (http://php.net/manual/en/install.php)
    Language Reference (http://php.net/manual/en/langref.php)
    Security (http://php.net/manual/en/security.php)
    Features (http://php.net/manual/en/Features)
    Function Reference (http://php.net/manual/en/funcref.php)
    PHP at the Core: A Hacker's Guide (http://php.net/manual/en/internals2.php)
    FAQ (http://php.net/manual/en/faq.php)
    Appendices (http://php.net/manual/en/appendices.php)
    [2016-12-17 13:44:33] parsing end
    [2016-12-17 13:44:34] saving to file langref.txt
    [2016-12-17 13:44:35] saved

作者:何广宇

链接:https://www.zhihu.com/question/19757909/answer/13621166

来源:知乎

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 答:

代码如下:

    <?php
    function request($config)
    {
        if (empty($config['url'])) {
            return false;
        }
    
        $ch = curl_init($config['url']);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if (!empty($config['https'])) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        }
        if (!empty($config['method']) && 'post' == $config['method']) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $config['postfields']);
        }
        $msg   = curl_exec($ch);
        $error = curl_error($ch);
        if (!empty($error)) {
            $msg = curl_errno($ch) . ': ' . $error;
        }
        curl_close($ch);
        return $msg;
    }
    $config['url'] = 'http://php.net/manual/en/langref.php';
    $str           = request(array('url' => 'http://php.net/manual/en/langref.php'));
    $reg           = '/<ul class=\'parent-menu-list\'>([\s\S]*?)<\/ul>/';
    preg_match($reg, $str, $liStr);
    $liStr = $liStr[1];
    $reg   = '/<a href="(.*?)".*?>(.*?)<\/a>/';
    preg_match_all($reg, $liStr, $finalStr);
    $str = '[' . date('Y-m-d H:i:s') . '] fetching ' . $config['url'] . PHP_EOL;
    echo '[' . date('Y-m-d H:i:s') . '] fetching ' . $config['url'] . '<br>';
    sleep(1);
    $str .= '[' . date('Y-m-d H:i:s') . '] parsing start' . PHP_EOL;
    echo '[' . date('Y-m-d H:i:s') . '] parsing start<br>';
    sleep(1);
    $str .= '[' . date('Y-m-d H:i:s') . '] the right side list is:' . PHP_EOL;
    echo '[' . date('Y-m-d H:i:s') . '] the right side list is:<br>';
    foreach ($finalStr[1] as $key => $value) {
        $str .= $finalStr[2][$key] . '(http://php.net/manual/en/' . $value . ')' . PHP_EOL;
        echo $finalStr[2][$key] . '(http://php.net/manual/en/' . $value . ')<br>';
    }
    sleep(1);
    $str .= '[' . date('Y-m-d H:i:s') . '] parsing end' . PHP_EOL;
    echo '[' . date('Y-m-d H:i:s') . '] parsing end<br>';
    sleep(1);
    $str .= '[' . date('Y-m-d H:i:s') . '] saving to file langref.txt' . PHP_EOL;
    echo '[' . date('Y-m-d H:i:s') . '] saving to file langref.txt<br>';
    file_put_contents('langref.txt', $str);
    sleep(1);
    echo '[' . date('Y-m-d H:i:s') . '] saved<br>';
    file_put_contents('langref.txt', '[' . date('Y-m-d H:i:s') . '] saved' . PHP_EOL, FILE_APPEND);
  • 第二题

创建一个数据库php_manual,新建表index,这个表有3个字段: id, title, link.
然后创建一个数据库用户php_manual_user,密码是php_manual_pass.
把上述数据库导出成sql,把SQL语句贴到下面,使得我们在mysql命令行终端里执行这些sql语句可以完成上述操作.

作者:何广宇

链接:https://www.zhihu.com/question/19757909/answer/13621166

来源:知乎

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 答:

代码如下:

#  前提是已经以root权限登录
#  删除原来已经存在同名数据库
drop database if exists `php_manual`;
#  创建数据库
create database `php_manual`;
#  选择需要操作的数据库
use `php_manual`;
#  删除原来已经存在的同名数据表
drop table if exists `index`;
# 创建数据表
create table `index` (
    id int unsigned auto_increment primary key,
    title varchar(255) not null,
    link varchar(255) not null
)engine=InnoDB auto_increment=1 default charset=utf8;
#  创建用户名密码
create user `php_manual_user`@'%' identified by 'php_manual_pass';
# 退出命令行模式
exit;
# 命令行下执行以下命令导出数据库
mysqldump -uphp_manual_user -pphp_manual_pass php_manual > D:/php_manual.sql
  • 第三题

写一个php脚本,读取第1题的结果langref.txt并解析出title和link,插入第2题创建的数据库表index里.

作者:何广宇

链接:https://www.zhihu.com/question/19757909/answer/13621166

来源:知乎

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 答:

代码如下:

  <?php
        //  从文件读取
        $title = $link = array();
        $fp    = fopen('langref.txt', 'r');
        while ($line = fgets($fp)) {
            if (strpos($line, ']')) {
                continue;
            }
            $strArr    = explode('(', $line);
            $title[]   = $strArr[0];
            $strArr[1] = trim($strArr[1]);
            $link[]    = str_replace(')', '', $strArr[1]);
        }
        fclose($fp);
        //  入库
        $conn = new mysqli('127.0.0.1', 'php_manual_user', 'php_manual_pass', 'php_manual');
        if ($conn->connect_error) {
            die('Error:' . $conn->connect_error);
        }
        foreach ($title as $key => $value) {
            $sql = 'INSERT INTO `index` (title, link) VALUES ("' . $value . '", "' . $link[$key] . '")';
            echo $sql;
            if (true === $conn->query($sql)) {
                echo 'Insert ' . $key . 'line successful<br>';
            } else {
                die('Cannot insert the data to mysql' . $conn->connect_error);
            }
        }
        $conn->close();
  • 第四题

使用jQuery写一个function来解析langref.php右侧的目录列表,运行这个function返回如下一个object.

作者:何广宇

链接:https://www.zhihu.com/question/19757909/answer/13621166

来源:知乎

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

{
    "Copyright": "http://php.net/manual/en/copyright.php",
    "PHP Manual": "http://php.net/manual/en/manual.php",
    "Getting Started": "http://php.net/manual/en/getting-started.php",
    "Installation and Configuration": "http://php.net/manual/en/install.php",
    "Language Reference": "http://php.net/manual/en/langref.php",
    "Security": "http://php.net/manual/en/security.php",
    "Features": "http://php.net/manual/en/Features",
    "Function Reference": "http://php.net/manual/en/funcref.php",
    "PHP at the Core: A Hacker's Guide": "http://php.net/manual/en/internals2.php",
    "FAQ": "http://php.net/manual/en/faq.php",
    "Appendices": "http://php.net/manual/en/appendices.php"
}
  • 答:

  • 第五题

写一条shell命令,执行此命令可获取到http://php.net/manual/en/langref.php的内容并将页面里的所有大写的PHP转成小写,最后将结果保存到/tmp/langref.html里.

作者:何广宇

链接:https://www.zhihu.com/question/19757909/answer/13621166

来源:知乎

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 答:

  • 第六题:

(加分题) 改写下边的脚本,使得当接收到SIGINT信号时打印出"caught signal SIGINT, exit"并退出.

<?php
    while (1) {
        echo "\n\n";
        echo "I am doing something important\n";
        echo "if i am interruptted, the data will be corrupted\n";
        echo "be careful\n";
        echo "\n\n";
        sleep(3);
    }

作者:何广宇

链接:https://www.zhihu.com/question/19757909/answer/13621166

来源:知乎

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 答:

  • 第七题:

(加分题) 有一个超级大的int数组要求和,假设有1000W,写一个php脚本,根据当前机器(假设是多核的)cpu的核数,fork出这么多子进程,把数组平分,每个子进程计算其中一部分,并把结果保存到/tmp/子进程pid.txt.
最后父进程汇总并输出求各结果.

作者:何广宇

链接:https://www.zhihu.com/question/19757909/answer/13621166

来源:知乎

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 答:
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 个人知乎收藏系列(4) 1.李燃末的回答## 看到这个问题竟然有些莫名的心酸。看了几个回答也有些隐隐的愤怒。 大学...
    独孤星夜的孤独等待阅读 5,323评论 0 23
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,930评论 25 708
  • 十八年来,经历了大大小小五花八门的失望。不算多,也少有彻底。 今天经历了一场迄今为止最深沉的失望。我是不想说出来的...
    远极独游阅读 102评论 0 0
  • 你突然的出现我没有防备 你是谁的骑士欲做谁护卫 心里要相遇或者与谁作对 全部放到一边而选择颓废 想问问对不对悔不悔...
    青菜萝卜豆腐汤阅读 415评论 2 0
  • 每个人。。。都有自己的一段过往,你永远不知道他的底线在哪里,他的容忍度有多大。若你无意触及那块伤疤,狠狠的揭下来,...
    夕年丶阅读 180评论 0 1