kk Blog —— 通用基础


date [-d @int|str] [+%s|"+%F %T"]
netstat -ltunp
sar -n DEV 1

JavaScript 实现搜索框联想功能

https://www.runoob.com/w3cnote/javascript-autocomplete.html

基础 HTML 代码

1
2
3
4
5
6
7
<!-- autocomplete="off" 确保表单已关闭自动填充功能: -->
<form autocomplete="off" action="/index.php">
	<div class="autocomplete" style="width:300px;">
		<input id="myInput" type="text" name="s" placeholder="请输入搜索内容">
	</div>
	<input type="submit">
</form>

以下搜索搜索框和联想菜单的样式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
.autocomplete {
	/*容器定位设置为 relative:*/
	position: relative;
	display: inline-block;
}
.autocomplete-items {
	position: absolute;
	border: 1px solid #d4d4d4;
	border-bottom: none;
	border-top: none;
	z-index: 99;
	/*设置自动填充项宽度与容器相同*/
	top: 100%;
	left: 0;
	right: 0;
}
.autocomplete-items div {
	font-size: 15px;
	padding: 1px;
	cursor: pointer;
	background-color: #fff;
	border-bottom: 1px solid #d4d4d4;
}
.autocomplete-items div:hover {
	/*鼠标移动到填充项设置的背景颜色*/
	background-color: #e9e9e9;
}
.autocomplete-active {
	/*使用箭头键浏览填充项时的背景颜色*/
	background-color: DodgerBlue !important;
	color: #ffffff;
}

以下是搜索搜索框和联想菜单的 JavaScript 代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
function autocomplete(inp, arr) {
	/*自动填充函数有两个参数,input 输入框元素和自动填充的数组*/
	var currentFocus;
	/* 监听 input 输入框,当在 input 输入框元素中时执行以下函数*/
	inp.addEventListener("input", function(e) {
		var a, b, i, val = this.value, an = 0;
		/*关闭已打开的自动填充列表*/
		closeAllLists();

		if (!val) { return false;}

		currentFocus = -1;
		/*创建 DIV 元素用于放置自动填充列表的值*/
		a = document.createElement("DIV");
		a.setAttribute("id", this.id + "autocomplete-list");
		a.setAttribute("class", "autocomplete-items");
		/*DIV 作为自动填充容器的子元素*/
		this.parentNode.appendChild(a);
		/*循环数组*/
		for (i = 0; i < arr.length; i++) {
			/*检查填充项是否有与文本字段值相同的内容,不区分大小写*/
			if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
				/*为每个匹配元素创建一个 DIV 元素 */
				b = document.createElement("DIV");
				/*匹配项加粗*/
				b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
				b.innerHTML += arr[i].substr(val.length);
				/*选中的填充项插入到隐藏 input 输入字段,用于保存当前选中值*/
				b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
				/*当有人点击填充项(DIV 元素)时执行函数*/
				b.addEventListener("click", function(e) {
					/*选中的填充项插入到隐藏 input 搜索字段*/
					inp.value = this.getElementsByTagName("input")[0].value;
					/*关闭自动填充列表*/
					closeAllLists();
				});
				a.appendChild(b);
				an ++;
				if (an > 20)
					break;
			}
		}
	});

	/*按下键盘上的一个键时执行函数*/
	inp.addEventListener("keydown", function(e) {
		var x = document.getElementById(this.id + "autocomplete-list");
		if (x) x = x.getElementsByTagName("div");
		if (e.keyCode == 40) {
			/*如果按下箭头向下键,currentFocus 变量加 1,即向下移动一位*/
			currentFocus++;
			/*使当前选中项更醒目*/
			addActive(x);
		} else if (e.keyCode == 38) { //up
			/*按下箭头向上键,选中列表项向上移动一位*/
			currentFocus--;
			/*使当前选中项更醒目*/
			addActive(x);
		} else if (e.keyCode == 13) {
			/*如果按下 ENTER 键,阻止提交,你也可以设置 submit 提交*/
			e.preventDefault();

			/* search_word(); */

			if (currentFocus > -1) {
				/*模拟点击选中项*/
				if (x) x[currentFocus].click();
			}
		}
	});

	function addActive(x) {
		/*设置选中的选项函数*/
		if (!x) return false;
		/*移动选项设置不同选中选项的背景颜色*/
		removeActive(x);
		if (currentFocus >= x.length) currentFocus = 0;
		if (currentFocus < 0) currentFocus = (x.length - 1);
		/*添加 "autocomplete-active" 类*/
		x[currentFocus].classList.add("autocomplete-active");
	}
	function removeActive(x) {
		/*移除没有选中选项的 "autocomplete-active" 类*/
		for (var i = 0; i < x.length; i++) {
			x[i].classList.remove("autocomplete-active");
		}
	}
	function closeAllLists(elmnt) {
		/*关闭自动添加列表*/
		var x = document.getElementsByClassName("autocomplete-items");
		for (var i = 0; i < x.length; i++) {
			if (elmnt != x[i] && elmnt != inp) {
				x[i].parentNode.removeChild(x[i]);
			}
		}
	}

	/*点击 HTML 文档任意位置关闭填充列表*/
	document.addEventListener("click", function (e) {
		closeAllLists(e.target);
	});
}

最后我们在 id 为 myInput 的 input 输入框使用以上 JavaScript 代码:

1
2
var sites = ["Google","Taobao","Runoob","Wiki","Zhihu","Baidu","Sina","Tmall","JD","Alibaba","QQ","Weixin"];
autocomplete(document.getElementById("myInput"), sites);

JS 获取 URL参数

https://www.runoob.com/w3cnote/js-get-url-param.html

1
2
3
4
5
6
7
8
9
10
function getQueryVariable(variable)
{
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if(pair[0] == variable){return pair[1];}
	}
	return(false);
}

PHP float浮点数的精度问题

https://blog.csdn.net/cr27225/article/details/118603543

https://www.cnblogs.com/phpfensi/p/8143367.html

1
2
3
4
5
6
7
$a = 2586;
$b = 2585.98;
var_dump($a-$b);

期望的结果是:float(0.02)

实际结果:float(0.019999999999982)

浮点数的精度有限。尽管取决于系统,PHP 通常使用 IEEE 754 双精度格式,则由于取整而导致的最大相对误差为 1.11e-16。非基本数学运算可能会给出更大误差,并且要考虑到进行复合运算时的误差传递。

此外,以十进制能够精确表示的有理数如 0.1 或 0.7,无论有多少尾数都不能被内部所使用的二进制精确表示,因此不能在不丢失一点点精度的情况下转换为二进制的格式。

这就会造成混乱的结果:例如,floor((0.1+0.7)*10) 通常会返回 7 而不是预期中的 8,因为该结果内部的表示其实是类似 7.9999999999999991118…

所以永远不要相信浮点数结果精确到了最后一位,也永远不要比较两个浮点数是否相等。如果确实需要更高的精度,应该使用任意精度数学函数或者 gmp 函数。

bc函数:

bcadd — 两个任意精度数字的加法计算

bccomp — 比较两个任意精度的数字

bcdiv — 两个任意精度的数字除法计算

bcmod — 任意精度数字取模

bcmul — 两个任意精度数字乘法计算

bcpow — 任意精度数字的乘方

bcpowmod — Raise an arbitrary precision number to another, reduced by a specified modulus

bcscale — 设置/获取所有 bc math 函数的默认小数点保留位数

bcsqrt — 任意精度数字的二次方根

bcsub — 两个任意精度数字的减法