面试题-使用IE的ActiveX对象过滤敏感词

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        // -------------- 全局变量,用来判断文本域中是否包含脏词,默认为false,即不包含脏词-------
        var isDirty = false;
     //读取本地dirtyword词库   
    function readFile(filename){
    /*函数编辑区*/
        try {
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            var file = fso.OpenTextFile(filename, 1);
            var fileContent = file.ReadAll();
            file.Close();
            return fileContent.split(' ');
        } catch (e) {
            alert('请开启ActiveX'); 
        }
    }

    

        /*
        * 提交表单的主方法
        * 在提交表单的时候对内容进行过滤并在文本域显示过滤后的内容
        */
        function submitForm1() {
            /*函数编辑区*/
            var messageValue = document.getElementById('message').value;
            messageValue = filterWord(messageValue);
            if (isDirty) {
            alert(messageValue);
                document.getElementById('message').value = messageValue;
                if (confirm('你的留言中含有不恰当的词语,系统已自动为你修改,是否继续提交?')) {
                    return true;
                }
                return false;
            }
            return true;
        }
        /*
        * 对传进来的messageValue过滤并返回新内容
        
        */
        function filterWord(messageValue) {
            // 根据文本域的id获取文本域对象内容
            /*函数编辑区*/
            var dirtyWords = readFile('c:/ciku.txt');
            for (var i = 0; i < dirtyWords.length; i++) {
                isDirty = true;
                messageValue = filterOneWord(messageValue, dirtyWords[i]);
            }
            return messageValue;
        }
        /*
        * 这个函数用来过滤单个词语, 如果messageValue中含有oneDirtyWord, 则用"**"替换这个oneDirtyWord
        * messageValue --- 要过滤的语句

        */
        function filterOneWord(messageValue, oneDirtyWord) {
        /*函数编辑区*/
        var regexp = new RegExp(oneDirtyWord,  'gi');
        return messageValue.replace(regexp, '**');
        }
 </script>
</head>
<body>
    <form name="message_board" id="message_board" action="aaa.html">
        <textarea name="message" id="message" cols="50" rows="10">
"In a world that's changing really quickly, the biggest risk is not taking any risk."
—— Mark Zuckerberg
        </textarea><br/>
        <input type="button" value="提交留言" id="submitMessage" onclick="submitForm1()"/>
    </form>
</body>
</html>
2017/2/21 posted in  面试题 PHP

安装composer的三种方式

2017/2/21 posted in  PHP

面试题-求多叉树两节点的最短路径

第一种解法

function findParentNode($nodes, $id)
{
    foreach ($nodes as $node) {
        if (in_array($id, $node['childrens'])) {
            return $node;
        }
    }
    return false;
}

function minimum2($nodes, $from, $to)
{
    $fromToTop = array($from);
    while ($parentNode = findParentNode($nodes, $from)) {
        $fromToTop[] = $parentNode['id'];
        $from = $parentNode['id'];
    }
    $toToTop = array($to);
    while ($parentNode = findParentNode($nodes, $to)) {
        $fromToTop[] = $parentNode['id'];
        $to = $parentNode['id'];
    }
    $path = false;
    $fromPath = array();
    $toPath = array();
    $countf = count($fromToTop);
    $countt = count($toToTop);
    for ($i=0; $i < $countf; $i++) {
        $fromPath[] = $fromToTop[$i];
        for ($j=0; $j < $countt; $j++) { 
            $toPath[] = $toToTop[$j];
            if ($fromToTop[$i] == $toToTop[$j]) {
                // 共同点。
                // 去掉一个共同点
                array_pop($fromPath);
                $path = implode('=>', $fromPath) . '=>' . implode('=>', $toPath);
                break 2;
            }
        }
        $toPath = array();
    }
    return $path;
}

第二种解法

function findParentNode($nodes, $id)
{
    foreach ($nodes as $node) {
        if (in_array($id, $node['childrens'])) {
            return $node;
        }
    }
    return false;
}

function minimum($nodes, $from, $to, $path = '', $tryParent = true)
{
    static $finds = array();
    static $excludeIds = array();
    if ($from == $to) {
        $path = $path . $from;
        $finds[] = $path;
        return $finds;
    }
    $path = $path . $from . '=>';

    $startNode = $nodes[$from];
    // 在子节点中找
    foreach ($startNode['childrens'] as $childId) {
        if (in_array($childId, $excludeIds)) {
            continue;
        }
        $childNode = $nodes[$childId];
        minimum($nodes, $childNode['id'], $to, $path, false);
    }

    $parentNode = findParentNode($nodes, $startNode['id']);
    $excludeIds[] = $startNode['id'];
    if ($tryParent && $parentNode) {
        minimum($nodes, $parentNode['id'], $to, $path, true);
    }

    usort($finds, function($a, $b) {
        $strLenA = strlen($a);
        $strLenB = strlen($b);
        if ($strLenA == $strLenB) {
            return 0;
        }
        return ($strLenA < $strLenB) ? -1 : 1;
    });
    return isset($finds[0]) ? $finds[0] : false;
}

测试

$nodes=array(
    array(
        'id' => 'A',
        'childrens' => array('B', 'C', 'D'),
    ),
    array(
        'id' => 'B',
        'childrens' => array('E', 'F', 'G'),
    ),
    array(
        'id' => 'C',
        'childrens' => array(),
    ),
    array(
        'id' => 'D',
        'childrens' => array('H', 'I', 'J'),
    ),
    array(
        'id' => 'E',
        'childrens' => array(),
    ),
    array(
        'id' => 'F',
        'childrens' => array('K', 'L', 'N'),
    ),
    array(
        'id' => 'G',
        'childrens' => array(),
    ),
    array(
        'id' => 'H',
        'childrens' => array('O', 'P', 'Q'),
    ),
    array(
        'id' => 'I',
        'childrens' => array(),
    ),
    array(
        'id' => 'J',
        'childrens' => array(),
    ),
    array(
        'id' => 'K',
        'childrens' => array(),
    ),
    array(
        'id' => 'L',
        'childrens' => array(),
    ),
    array(
        'id' => 'N',
        'childrens' => array('R', 'S', 'T'),
    ),
    array(
        'id' => 'O',
        'childrens' => array(),
    ),
    array(
        'id' => 'P',
        'childrens' => array(),
    ),
    array(
        'id' => 'Q',
        'childrens' => array(),
    ),
    array(
        'id' => 'R',
        'childrens' => array(),
    ),
    array(
        'id' => 'S',
        'childrens' => array(),
    ),
    array(
        'id' => 'T',
        'childrens' => array(),
    ),
);
$idNodeMap = array();
foreach($nodes as $key => $value) {
    $idNodeMap[$value['id']] = $value;
}

var_dump(minimum($idNodeMap, 'T', 'A'));
var_dump(minimum2($idNodeMap, 'T', 'A'));
2017/2/14 posted in  面试题 PHP

swoole项目service模板

2016/7/5 posted in  PHP OPS

xhprof安装小记

xhprof安装步骤 https://github.com/longxinH/xhprof-apm/blob/php7/README_CN.md

1. 关闭opcache扩展
2. 增加php的内存限制到4GB

xgui安装步骤:https://github.com/perftools/xhgui

array {
    [函数名]=>
      array {
        ["ct"] => 调用次数 (int)
        ["wt"] => 函数方法执行的时间耗时 (int)
        ["cpu"] => 函数方法执行消耗的cpu时间 (int)
        ["mu"] => 函数方法所使用的内存 (int)
        ["pmu"] => 函数方法所使用的内存峰值 (int)
        ["files"] => array {
          [文件名] =>
            array {
              [行号] => 调用次数 (int)
            }
        }
    }
}
2016/5/20 posted in  PHP

三分钟实现程序分库分表

2014/7/6 posted in  PHP SQL