Skip to content

Instantly share code, notes, and snippets.

View liuxd's full-sized avatar
🗿
Hi, there.

Allen Liu liuxd

🗿
Hi, there.
  • Tax Traders
  • Auckland, New Zealand
View GitHub Profile
@liuxd
liuxd / HeapSort.php
Created September 7, 2017 01:12
HeapSort.php
<?php
/**
* Heap Sort.
*
* Keywords: heap
* Time Complexity: O(n log n)
* Space Complexity: O(1)
*/
class HeapSort extends SplHeap
{
@liuxd
liuxd / BinarySearch.php
Created September 7, 2017 01:12
BinarySearch.php
<?php
/**
* Searching a value from a sorted array with dichotomy method.
*
* Keywords: median
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* @param int $target The value you want to find.
* @param array $sorted_array The sorted array you want to search.
@liuxd
liuxd / Swap.php
Created September 7, 2017 01:11
Swap.php
<?php
/**
* Swap two variables' values without extra variable.
*/
# Solution 1:
function solution1 ($a, $b)
{
$a = $a + $b - ($b = $a);
echo "solution1 :", PHP_EOL;
var_dump($a, $b);
@liuxd
liuxd / xml2arr.php
Created September 7, 2017 01:11
xml2arr.php
<?php
/**
* xml字符串转成数组。
*
* @param string $xml
* @return array
*/
function xml2arr ($xml)
{
libxml_disable_entity_loader(true);
@liuxd
liuxd / weekTotal.php
Created September 7, 2017 01:10
weekTotal.php
<?php
/**
* 计算某年有多少周。按每周以星期一开始来计算。
* @param int $year
* @return int
*/
function weekTotal($year)
{
/**
* 计算某个日期是星期几。
@liuxd
liuxd / twoFieldSorter.php
Created September 7, 2017 01:10
twoFieldSorter.php
<?php
/**
* 二维数组(例如:从数据读取一个数据列表。)按两个字段进行排序。使用冒泡排序。
*
* @param array $arr 待排序数组。
* @param array $ord 排序因子。
* 例子:[
* 0 => 'field1:asc',
* 1 => 'field2:desc',
* ];
@liuxd
liuxd / remove_not_utf8.php
Created September 7, 2017 01:10
remove_not_utf8.php
<?php
/**
* 用正则将所有`UTF8`字符拿出来,扔进一个数组。
* 将数组的所有字符连接起来,构成结果字符串。
*/
/**
* 处理函数
* @param string $str 待过滤字符串。
* @return string
*/
@liuxd
liuxd / post.php
Created September 7, 2017 01:09
post.php
<?php
/**
* PHP三种POST数据的方式。
*/
class PHPPost
{
/**
* 通过cUrl扩展的POST。
* @param string $url
* @param array $post_data
@liuxd
liuxd / phar-packer.php
Created September 7, 2017 01:09
phar-packer.php
#!/usr/bin/env php
<?php
// php.ini : `phar.readonly = Off`
$longopts = array(
'name::',
'path::',
'init:'
);
$opts = getopt('', $longopts);
$name = (isset($opts['name'])) ? $opts['name'] . '.phar' : '';
@liuxd
liuxd / json_prepare.php
Created September 7, 2017 01:09
json_prepare.php
<?php
/**
* json_decode() 如果返回 'null',说明触犯了以下规则:
* 1. 包含非UTF-8编码的字符。
* 2. 在最后元素有逗号。
* 3. 使用单引号包含元素的值。
* 4. 包含了`ASCII`码的`0~31`代表的不可见符号。
* 5. 包含了微软编辑器特有的`BOM`头。
*
* 关于第4、5点问题,可以用以下函数处理: