strlen

PHP的经典常用算法值得收藏

匿名 (未验证) 提交于 2019-12-02 22:11:45
<? //-------------------- // 基本数据结构算法 //-------------------- //二分查找(数组里查找某个元素) function bin_sch ( $array , $low , $high , $k ){ if ( $low <= $high ){ $mid = intval (( $low + $high )/ 2 ); if ( $array [ $mid ] == $k ){ return $mid ; } elseif ( $k < $array [ $mid ]){ return bin_sch ( $array , $low , $mid - 1 , $k ); } else { return bin_sch ( $array , $mid + 1 , $high , $k ); } } return - 1 ; } //顺序查找(数组里查找某个元素) function seq_sch ( $array , $n , $k ){ $array [ $n ] = $k ; for ( $i = 0 ; $i < $n ; $i ++){ if ( $array [ $i ]== $k ){ break ; } } if ( $i < $n ){ return $i ; } else { return - 1 ; } } /

PHP数字金额转换大写金额

匿名 (未验证) 提交于 2019-12-02 22:11:45
早些年做CRM用到的一个金额转换函数,今天从旧项目中拿出来记录一下。金额转换的函数方法有很多,都很不错。不过这个是小崔刚工作的时候写的一个转换函数,多少还是有点纪念意义。如有问题请朋友们指出,小崔及时修正。谢谢啦! 废话不多说直接上代码: 1 <?php 2 3 /** 4 * 数字金额转换大写数字 5 * $num 数字类型 6 */ 7 8 function inttodaxie($num) { 9 //判断$num是否数字 10 if(!is_numeric($num)) return -1; 11 $dint = array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'); 12 $len = strlen($num); 13 $dstr = ''; 14 for($i = 0; $i <= $len; $i++) { 15 $key_ = substr($num, $i, 1); 16 $dstr .= $dint[$key_]; 17 } 18 return $dstr; 19 } 20 21 $result = inttodaxie(90011234); 22 var_dump($result); 23 echo '<br>'; 以上是基础转换代码,在这个基础上进行二次改造: 1 <?php 2 3 /** 4 *

base64文件大小计算

匿名 (未验证) 提交于 2019-12-02 21:53:52
有时候图片被 base64 之后需要计算图片大小,因为被编码后全是字符,计算文件大小可以反序列化成文件之后再获取大小,但是会比较麻烦。简单介绍一种利用 base64 编码原理计算大小的方法. 要求把 3 个 8 位字节 (3*8=24) 转化为 4 个 6 位的字节 (4*6=24) ,之后在6位的前面补两个0,形成8位一个字节的形式。 如果剩下的字符不足 3 个字节,用 0 填充,输出字符使用 ’=’ ,因此编码后输出的文本末尾可能会出现 1 或 2 个 ’=’ 找一张图片文件 https://www.baidu.com/img/bd_logo1.png base64 编码之后的图片显示结果如下 data:image/png;base64, var baseStr=document.getElementById("imgcase").getAttribute("src"),tag="base64,"; baseStr=baseStr.substring(baseStr.indexOf(tag)+tag.length); 去掉base64编码中的“ = ”号   var eqTagIndex=baseStr.indexOf("="); baseStr=eqTagIndex!=-1?baseStr.substring(0,eqTagIndex):baseStr; 计算文件流大小

PHP实现AES加密解密

两盒软妹~` 提交于 2019-12-02 21:30:38
1、mcrypt_encrypt AES加密,解密 1 class Lib_desEnctyp 2 { 3 private $key = ""; 4 private $iv = ""; 5 6 /** 7 * 构造,传递二个已经进行base64_encode的KEY与IV 8 * 9 * @param string $key 10 * @param string $iv 11 */ 12 function __construct ($key, $iv) 13 { 14 if (empty($key) || empty($iv)) { 15 echo 'key and iv is not valid'; 16 exit(); 17 } 18 $this->key = $key; 19 $this->iv = $iv; 20 } 21 22 /** 23 *加密 24 * @param <type> $value 25 * @return <type> 26 */ 27 public function encrypt ($value) 28 { 29 $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, ''); 30 $iv = base64_decode($this->iv); 31 $value = $this-

字符串匹配(部分整理)

跟風遠走 提交于 2019-12-02 19:12:13
**加载本页面时,公式可能未渲染(显示乱码)请刷新该页面重试* 问题:给出两串字符 \(text[]="adsgwadsxdsgwadsgz"\) 长度为N \(pattern[]="dsgwadsgz"\) 长度为M 查找pattern字符串是否在text出现过并输出所在位置。 一、Brute-Force朴素匹配算法(暴力查找) 时间复杂度 \(O(MN)\) 很简单粗暴,碰到稍微长一点的就。。。。。。 图解 代码样例 int i,j for(i=0; i < strlen(text); i++){ for(j=0; j < strlen(pattren); j++){ if(text[i] == pattern[j]) break; } if(j == strlen(pattren)){ printf("Found pattern at %d\n",i-j); } } 二、Rabin-Karp算法(哈西匹配) 时间复杂度 \(O(MN)\) ,但是实际的速度要更快平均为 \(O(M+N)\) 参考OIjulao闫老师的 (B站ID: 48833491 )教学视频 哈希算法就是将一个字符串中的任意子串映分别射到一个实数上面(求子串的时间复杂度为 \(O(1)\) ) 哈希值的求法就是将字串的p进制数去模q(p常取131、13331,q常取264时哈希值的重复概率要小,

PHP隐藏身份证号中间四位(适用老版15位与18位身份证号)

微笑、不失礼 提交于 2019-12-02 15:36:38
// $message['idcard'] 为数据库查出来的身份证号码,若位数不符合身份证位数则会显示“身份证位数不正常” <? echo strlen($message['idcard'])==15?substr_replace($message['idcard'],"****",8,5):(strlen($message['idcard'])==18?substr_replace($message['idcard'],"****",8,7):"身份证位数不正常!"); ?> 来源: https://www.cnblogs.com/yite/p/11751894.html

Checking string length with strlen

微笑、不失礼 提交于 2019-12-02 13:44:56
I have this code, i want it to check if the length is NOT 32 or 40. The code below only checks if the word is 32 or 40 in length. <? $name = mysql_real_escape_string($_POST['dgt']); $len = strlen($name); if (!($len == "32" or $len == "40")){ print "This is not a name."; } else { echo 'This is a name'; ?> if ($len != 32 && $len != 40) try that out What about if($len != 32 and $len != 40) <?php $name = mysql_real_escape_string($_POST['dgt']); $len = strlen($name); if ($len != 32 && $len != 40){ print "This is not a name."; } else { echo 'This is a name'; ?> 来源: https://stackoverflow.com

String is longer than expected in C

那年仲夏 提交于 2019-12-02 13:16:57
问题 Here's my code #include <stdio.h> #include <string.h> int main(){ char pal[8] = "ciaooaic"; char pal1[7] = "ciaoaic"; int lenPal = strlen(pal); int lenPal1 = strlen(pal1); printf("strlen('%s'): %d\n", pal, lenPal); printf("strlen('%s'): %d\n", pal1, lenPal1); return 0; } The problem is that when I run this code the output is: strlen('ciaooaicP@'): 11 strlen('ciaoaic'): 7 The first string has also another non-printable char between P and @. I'm a noob, so maybe I missed something obvious. Can

Php length of cyrillic string doubles its value

家住魔仙堡 提交于 2019-12-02 07:25:48
问题 Hello here it is the problem: when i get to the $_POST latin string strilen() works perfectly, but when i get cyrillic string strlen() doubles its value here is the code: $word = $_POST['word']; echo strlen($word) . '<br>'; //input: abc -> returns 3, input: абв -> returns 6 var_dump($word); //input: abc -> returns string 'abc' (length=3), input: абв -> returns string 'абв' (length=6) Do you have some ideas?! 回答1: strlen does not double anything, it simply reports what the situation is.

what should strlen() really return in this code?

痞子三分冷 提交于 2019-12-02 04:10:54
问题 #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char qq[] = {'a' , 'b' , 'c' , 'd'}; char qqq[] = "abcd"; printf("%d\n" , sizeof qq / sizeof qq[0]); // line A printf("%d\n" , strlen(qq)); // line B printf("%d\n" , sizeof qqq / sizeof qqq[0]); // line C printf("%d\n" , strlen(qqq)); // line D system("pause"); return 0; } Look at the code above. The array qq has four elements in it and the array qqq has five elements in it the last of which is '\0' . So I understand