php关联数组快速排序<? function qsort($a,$f) { qsort_do(&$a,0,Count($a)-1,$f); } function qsort_do($a,$l,$r,$f) { if ($l < $r) { qsort_partit……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2438浏览 2217个赞
一个用来防止sql注入的参数检查函数function cleanuserinput($dirty){ if (get_magic_quotes_gpc()) { $clean = mysql_real_escape_string(stripslashes($dirty)); }else{ $clean = mysql_real_esca……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1933浏览 2857个赞
php中通过 func_num_args函数获取当前函数的参数数量,传入指定函数的参数数量可以是不固定的,通过func_num_args可以轻松获得参数个数function variable_argument_function () { $num_args = func_num_args(); echo ("I was passed $……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2430浏览 875个赞
php中输出大数据文本的方法ob_start(); echo 'Put your text here'; $body = ob_get_contents();ob_end_clean(); echo $body;……继续阅读 » 水墨上仙 4年前 (2021-03-10) 3017浏览 2205个赞
func_get_args php中用于返回指定函数的参数数量的函数,如果你想得到某个函数的参数个数的话,这个一定能帮到你。<?phpfunction foo(){ $numargs = func_num_args(); echo "Number of arguments: $numargs<br />\n……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2907浏览 1770个赞
可以让php随意传递参数的函数func_get_args和func_num_args php用户自定义的函数大部分是用来处理数据的,既然是处理数据那么就必然会有参数传入函数,一般的形式是这样的// function with 2 optional argumentsfuncti……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2231浏览 2140个赞
如果你需要一个随机字符串,下面这个函数正好帮你,可以自己定义长度function getCode($length=12) { //Ascii Code for number, lowercase, uppercase and special characters $no = range(48,57); $lo = range……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2707浏览 1329个赞
分享一个php清除html标签的函数,用于清除不需要的html标签格式function strip_html_tags( $text ){ $text = preg_replace( array( // Remove invisible content '@<head[……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2602浏览 832个赞
PHP连接mongodb的简单范例,改代码简单演示了php如何链接到本地mongodb数据库,并检索其中一个表的数据$connection = new Mongo( “localhost:27017” ); $collection = $connection->mydb->mycollection; $cursor = $collecti……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1219浏览 2546个赞
php通过正则表达式进行字符串搜索的简单范例<?php$string_to_search = "w3mentor.com";$regex = "/tor/";$num_matches = preg_match($regex, $string_to_search); if ($num_matches ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1674浏览 687个赞
php中 explode 和 split 函数用来分割字符串。 explode函数语法如下explode(substring, string) explode函数通过子字符串进行分割,效率比split要高 ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2756浏览 2157个赞
php中的count函数用来获得数组的长度,用法如下<?php $array = array("PHP", "Perl", "Java");print_r("Size 1: ".count($array)."\n");$array = arra……继续阅读 » 水墨上仙 4年前 (2021-03-10) 3156浏览 561个赞
php中可以通过ucfirst函数将一个字符串中的第一个字母转换成大写,而ucwords函数可以将一个字符串中每个单词的首字母转换成大写<?php$string = "php string functions are easy to use.";$sentence = ucfirst($string);$title = u……继续阅读 » 水墨上仙 4年前 (2021-03-10) 3080浏览 135个赞
php中可以通过strtolower和strtoupper两个函数将字符串中的每个英文字符全部转换成小写或者大写<?php$string = "Learn PHP string functions at 75271.com";$lower = strtolower($string);$upper = strtoupper(……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1816浏览 437个赞
php中可以通过bin2hex函数将字符串转换成16进制的形式输出,bin2hex()函数返回结果为ascii码<?php$string = "Hello\tworld!\n";print($string."\n");print(bin2hex($string)."\n");?&g……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2952浏览 846个赞
implode函数可以将存储在数组里的多个字符串按照指定的格式和分隔符连接成一个字符串<?php $date = array('01', '01', '2006');$keys = array('php', 'string', 'fun……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1931浏览 2080个赞
下面的代码检查一个字符串是否包含任何7位GSM字符。它对短信平台上工作的人非常有用。<?php function check_gsm($str) { $arr = array( "0x00", "0x01", "0x02", "0x03", &quo……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1614浏览 1542个赞
php通过asort()给关联数组按照值排序,和sort的区别是,sort是按照$nums = array("one"=>5,"two"=>2,"three"=>1);asort( $nums ); foreach ( $nums as $key => $val )……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2508浏览 131个赞
array_slice(array,offset,length,preserve) array_slice() 返回根据 offset 和 length 参数所指定的 array 数组中的一段序列。如果 offset 非负,则序列将从 array 中的此偏移量开始。如果 offset 为负,则序列将从 array 中距离末端这么远的地方开始。如果给出了 ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2174浏览 147个赞
strpos用来查找一个字符串在另一个字符串中首次出现的位置,strpos区分大小写,如果没有找到则返回false,所以strpos有两种类型的返回值,一种是整形,一种是bool型,开发过程中需要注意<?phpecho strpos("Hello world!","wo");?> ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2173浏览 518个赞
php中包含四个可以去除字符串空格的函数:trim() – 去除字符串两端的空字符ltrim() – 去除字符串前端的空字符rtrim() – 去除字符串末尾的空字符chop() –同rtrim().<?php$text = "\t \t 75271.com!\t \t ";$leftTrimmed = ltrim(……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2503浏览 1522个赞
对strpos()函数可以用来在php中查找子字符串。strpos()函数将试图找到子字符串在源字符串中首次出现的位置。如果找到了,它会返回一个非负整数表示子字符串出现的位置。 否则它会返回一个布尔值false。<?php$haystack1 = "2349534134345w3mentor16504381640386488129&qu……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2688浏览 2356个赞
下面的代码演示了php中的函数参数如何设置和使用默认值 <html> <head> <title> Printing text on a Web Page</title> </head> <body> <?php function textonw……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2548浏览 1748个赞
php中可以通过opendir打开指定目录,读取目录下的所有文件,如果读取失败返回False<?php$dir = "PUT_PATH_TO_DIR_HERE"; // Open a known directory, and proceed to read its contentsif (is_dir($dir)) {……继续阅读 » 水墨上仙 4年前 (2021-03-10) 3139浏览 1313个赞
php通过ksort()函数给关联数组按照键排序,ksort函数按照关联数组的key正序排序,如果要倒序可以是哦那个krsort()函数$first = array("x"=>5,"a"=>2,"f"=>1);ksort( $first ); foreach ( $fir……继续阅读 » 水墨上仙 4年前 (2021-03-10) 3142浏览 2579个赞
php中可以通过function_exists()函数检测另外一个函数是否存在,可以把函数名作为一个字符串传入function_exists,判断该还是是否存在function highlight( $txt ) { return "<sub>$txt</sub>";} function textWra……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1375浏览 2875个赞
php根据指定的位置和长度获得子字符串,php的substr函数功能非常强大,不断可以从前往后去子字符串还可以从后往前取字符串<?php$string = "beginning";print("Position counted from left: ".substr($string,0,5)."……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2252浏览 1114个赞
在python和golang中都有一个函数同时返回多个值的方法,其实php也可以,但相比python和golang要稍微麻烦一点,下面是一个简单的演示范例,这里用到了list函数<?php function retrieve_user_profile() { $user[] = "Jason"; $……继续阅读 » 水墨上仙 4年前 (2021-03-10) 3037浏览 382个赞
substr_replace用于在指定字符串中替换指定位置的子字符串<?php$string = "Warning: System will shutdown in NN minutes!";$pos = strpos($string, "NN");print(substr_replace($strin……继续阅读 » 水墨上仙 4年前 (2021-03-10) 3016浏览 659个赞
php可以通过rmdir()函数删除服务器上的目录,下面代码里用到了is_dir()函数,该函数用于判断指定的字符串是否是目录,删除成功返回True,否则返回False<?phpif (!is_dir('exampledir')) { mkdir('exampledir');} rmdir(&……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1992浏览 267个赞
php中可以通过usleep()函数让脚本暂停顺序执行一段时间,参数单位为毫秒<? usleep(4000000); echo "Done\n";?>……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2817浏览 258个赞
php中可以通过set_time_limit()函数限制脚本的最长执行时间,单位为秒<? set_time_limit(30); //要执行的代码?> 上面的代码控制脚本最长只能执行30秒,30秒后将抛出异常……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2516浏览 1526个赞
php使用递归函数实现数字累加,下面是一个简单的范例<?function summation ($count) { if ($count != 0) : return $count + summation($count-1); endif;}$sum = summation(10);print &qu……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1543浏览 2386个赞
linux下php可以通过mkdir创建目录,并制定目录访问权限mkdir( "testdir", 0777 ); // global read/write/execute permissionsmkdir( "testdir", 0755 ); // world and group: read/execute ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2930浏览 238个赞
sort()函数用于给数组排序,本函数为数组中的单元赋予新的键名。原有的键名将被删除。如果成功则返回 TRUE,否则返回 FALSE。$alpha = array ("x", "a", "f", "c");sort( $alpha ); foreach ( $alph……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1258浏览 1559个赞
wordwrap()函数可以按照指定的固定行长度格式化文本段落,让段落看起来更加整齐<?php$string = "TRADING ON MARGIN POSES ADDITIONAL RISKS AND IS NOT SUITABLE FOR ALL INVESTORS. A COMPLETE LIST OF T……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2080浏览 2711个赞
php通过strlen()函数计算字符串的长度<?php print(strlen('It\'s monday!'));?> 上面的代码输出结果:12……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2136浏览 2269个赞
php codeigniter中一次性加载多个view的方法function somecontrollerfunction(){$data['pagetitle'] = "Welcome to w3mentor.com";$this->load->view('pageheader……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2182浏览 762个赞
php链接mysql并保存email和username的范例代码<?php //opens connnection to mysql server $dbc = mysql_connect('localhost','root','');if (!dbc){ die(……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2790浏览 895个赞
php给每个段落添加空格的代码<?php //Prepends whitespace to each line of a string function white_space( $string, $whitespace ) { //Create an array from the string, each key having……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2922浏览 706个赞
headers_list函数没有参数,并返回一个数组。返回的数组包含一个数字索引表,包含了要发送给客户端的header信息<?php header("Expires: Sat, 12 Dec 1989 05:30:00 GMT"); echo "This is some output.&……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1392浏览 365个赞
php使用mysqli向数据库添加数据$mydb = new mysqli('localhost', 'username', 'password', 'databasename');$sql = "INSERT INTO users (fname, lname, ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1163浏览 205个赞
php通过mysqli接口检索和显示数据的代码$mydb = new mysqli('localhost', 'username', 'password', 'databasename');$sql = "SELECT * FROM users ORDER BY ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 3065浏览 955个赞
php codeigniter views中通过循环显示数组数据 controller如下:<?phpclass SimpleController extends Controller{function index() { $data['my_lis……继续阅读 » 水墨上仙 4年前 (2021-03-10) 1738浏览 1863个赞
双向循环队列-php实现历史记录的前进后退等功能<?php include 'debug.php';/*** 历史记录操作类* 传入或者构造一个数组。形如:array( 'history_num'=>20, //队列节点总共个数 'first'=>0, ……继续阅读 » 水墨上仙 4年前 (2021-03-10) 2065浏览 296个赞