Skip to content

Instantly share code, notes, and snippets.

@zigo928
zigo928 / removeSymol.php
Last active August 3, 2018 02:02
php正则,删除字符串中的中英文标点符号
<?php
$str = "!@#$%^&*(中'文:;﹑•中'文中'文().,<>|[]'\"";
//中文标点
$char = "。、!?:;﹑•"…‘’“”〝〞∕¦‖— 〈〉﹞﹝「」‹›〖〗】【»«』『〕〔》《﹐¸﹕︰﹔!¡?¿﹖﹌﹏﹋'´ˊˋ―﹫︳︴¯_ ̄﹢﹦﹤‐­˜﹟﹩﹠﹪﹡﹨﹍﹉﹎﹊ˇ︵︶︷︸︹︿﹀︺︽︾ˉ﹁﹂﹃﹄︻︼()";
$pattern = array(
"/[[:punct:]]/i", //英文标点符号
'/['.$char.']/u', //中文标点符号
'/[ ]{2,}/'
@zigo928
zigo928 / my_mb_strlen.php
Last active July 16, 2018 09:18
获取中英文混合字符串长度
<?php
function my_mb_strlen($value)
{
// 计算单字节.
preg_match_all('/[a-zA-Z0-9_]/', $value, $single);
$single = count($single[0]) / 2;
// 多子节长度.
$double = str_word_count(preg_replace('([a-zA-Z0-9_])', '', $value));
@zigo928
zigo928 / nginx-location
Created March 7, 2016 07:25 — forked from luxixing/nginx-location
nginx location 匹配规则
1 普通匹配,遵循最长匹配规则,假设一个请求匹配到了两个普通规则,则选择匹配长度大的那个
例如:
location /{
[matches]
}
location /test{
[matches]
}
2 精确匹配
location = /{
@zigo928
zigo928 / Flexihash.php
Created October 27, 2014 05:09
一致性hash算法实现-PHP版
<?php
/**
* Flexihash - A simple consistent hashing implementation for PHP.
*
* The MIT License
*
* Copyright (c) 2008 Paul Annesley
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@zigo928
zigo928 / binarySearch.php
Created October 22, 2014 09:40
二分查找实现
<?php
/**
* 二分查找
*
* @param $a array
* @param $v 查找的值
*
* @return int
*/
function binarySearch($a, $v)
@zigo928
zigo928 / longestCommonSequence.php
Created October 22, 2014 08:10
longest common sequence
<?php
function longestCommonSequence($str_a, $str_b)
{
assert("is_string('$str_a')");
assert("is_string('$str_b')");
$str_a_len = strlen($str_a);
$str_b_len = strlen($str_b);
$opt = array_fill(0, $str_a_len + 1, array_fill(0, $str_b_len + 1, 0));
@zigo928
zigo928 / isPrime.php
Last active August 29, 2015 14:07
找出素数
<?php
function isPrime($num)
{
//1 is not prime. See: http://en.wikipedia.org/wiki/Prime_number#Primality_of_one
if($num == 1)
return false;
//2 is prime (the only even number that is prime)
if($num == 2)
@zigo928
zigo928 / longest_common_substring.php
Last active August 29, 2015 14:07
找出存在多个字符串中的连续的最长的子字符串
<?php
function longest_common_substring($words)
{
$words = array_map('strtolower', array_map('trim', $words));
$sort_by_strlen = create_function('$a, $b', 'if (strlen($a) == strlen($b)) { return strcmp($a, $b); } return (strlen($a) < strlen($b)) ? -1 : 1;');
usort($words, $sort_by_strlen);
// We have to assume that each string has something in common with the first
// string (post sort), we just need to figure out what the longest common
// string is. If any string DOES NOT have something in common with the first
// string, return false.
@zigo928
zigo928 / maxDivisor.php
Last active August 29, 2015 14:07
最大公约数
<?php
function maxDivisor($a, $b)
{
if (! $a || !$b) {
return false;
}
if ($b > $a) {
list($a, $b) = array($b, $a);
@zigo928
zigo928 / longestCommonSubstring.php
Last active August 29, 2015 14:07
两个字符串都存在的最长的相同的字符串
<?php
function longestCommonSubstring($str_a, $str_b, $case_sensitive = false)
{
$len_a = mb_strlen($str_a);
$len_b = mb_strlen($str_b);
if (! $len_a || ! $len_b) {
return false;
}