Home > Article > Backend Development > Summary of common PHP functions and function libraries
First of all, I will introduce some relatively simple but essential and practical knowledge. It can be used as a manual for reference and is suitable for novices like me.
Introduction to common PHP library functions
1. Common functions for PHP string operations
1. Determine the string length
int strlen(string str)
2. Compare two strings
a. The strcmp function performs a binary-safe comparison of two strings and is case-sensitive
int strcmp(string str1,string str2)
b. Compare two strings in a case-insensitive manner
int strcasecmp(string str1,string str2)
3.Find the same parts of two strings
int strspn(string str1,string str2)
4. Find the different parts of two strings
5.int strcspn(string str1,string str2)
6. Handle the case of strings
a. Convert all strings to lowercase
string strtolower(string str)
b. Convert all strings to uppercase
string strtoupper(string str)
c. Capitalize the first character of the string
string ucfirst(string str)
d. Convert the first character of each word in the string to uppercase
string ucwords(string str)
7. Convert strings to HTML
a. Convert newlines to HTML termination tags
string bl2br(string str)
b. Convert special characters to wildHTML equivalents (no format parsing)
string htmlentities(string str[,int quote_style[ ,int charset]])
string htmlspecialchars(string str[,int quote_style[,string charset]])
c. Convert HTML to plain text, remove all php and html Tags
string strip_tags(string str[,string allowable_tags])
d. Convert text to HTML equivalent
array get_html_translaction_table(int table[,int quote_style])
e. Create a self- Defined conversion list
string strtr(string str,array replacements)
8.Regular expressionReplacement function of function
a. The strtok function parses according to a predefined string list String
string strtok(string str,string tokens): Returns all content until tokens are encountered
b. Analyze string according to predefined delimiters
array explode(string separator,string str [,int limit]): Split string
c. Convert array to string
string implode(string delimiter, array array)
d. Find the first occurrence of string
int strpos(string str,string substr[,int offset])
e. Find the last occurrence of the string
int strrpos(string str,char substr[,offset])
f. Use another character String replacement for all instances of string
mixed str_replace(string occurrence,mixed replacement,mixed str[,int count])
g. Get a part of the string strstr returns the first occurrence of a predefined string in the string The remaining part of the beginning
string strstr(string str,string occurrence)
h. Return a part of the string according to the predefined offset
string substr(string str,int start[,ing length]): start Can be a negative number, indicating the beginning of the countdown
i. Determine the frequency of occurrence of a string
int substr_count(string str,string substring)
j. Replace part of a string with another string
string substr_replace(string str,string replacement,int start[,int length])
9. Filling and removing strings
a. Cut characters from the beginning of the string
string ltrim(string str[,string charliset])
b. Trim characters from the end of the string
string rtrim(string str[,string charliset])
c. Trim characters from both ends of the string
string trim(string str[,string charliset])
d. Fill string
string str_pad(string str,int length[,string pad_string[,int pad_type]])
10.Character and word count
a. Count of characters in the string
mixed count_chars(string str[,mode])
b. Count of total words in the string
mixed str_word_count(string str[,mode] int format])
2. Three form validation functions commonly used in PHP Web development
(1) isset(); - suitable for detecting whether this parameter exists. Used to avoid referencing non-existent variables
Definition and scope: used to test whether a variable has a value (including 0, FALSE, or an empty string returns true, but cannot be NULL), that is: "http://localhost/?fo=" also passes the test, so it is not applicable. But if the "http://localhost/" parameter does not contain the fo parameter, you can use isset to detect it. At this time, isset($_GET['fo']) returns false
does not apply to: This function is not suitable for efficient way of validating text in html form. To check whether the user input text is valid, you can use empty();
(2)empty(); - the best function to check whether the variable has a null value
Definition and scope: used to check whether the variable has a null value: including: empty string, 0, null or false, these all return false, that is: "http://localhost/?fo=" or "http:// localhost/?fo=0", the results detected by empty are all true
Not applicable: not applicable to detecting parameters that can be 0
(3)is_numeric();——检查变量是否为数字
定义和作用范围:检查变量是否为数字,只适用于检测数字
不适用范围:但假如参数名不存在,会出错,因此不适合于第一层检测
另外还有一个好用的验证函数是checkdate(month,day,$year),用来确认某个日期是否存在或在过去是否存在
综合示例:
这是表单:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>表单验证示例</title> </head> <body> </body> <p> <a href="?fo=Jack">传有效值</a> <a href="?fo=">传空值</a> <a href="?fo=0">传0值</a> <br /><br /> <a href="?sex=m">性别:男</a> <a href="?sex=f">性别:女</a> <br /><br /> <a href="/">清空</a> <br /><br /> <input type="text" value="<?php echo $_GET['fo']!=''?$_GET['fo']:'';?>" size="155" /> </p> </html>[code] 这是验证 [code]<?php ini_set("display_errors",1); //ini_set(" error_reporting ",E_ALL); print_r error_reporting(E_ALL); $a=NULL; if(isset($a))echo '变量$a的isset为真'; echo '<h2>isset的情形:</h2>'; if(isset($_GET['fo'])){ echo '变量\'fo\'的isset为真,变量可用'; }else{ echo '变量\'fo\'的isset为假,无变量设置'; } echo '<h2>empty的情形:</h2>'; if(empty($_GET['fo'])){ echo '变量\'fo\'的empty为真,即空值或无效值'; }else{ echo '变量\'fo\'的empty为假,有值'; } echo '<h2>is_numeric的情形:</h2>'; if(is_numeric($_GET['fo'])){ //在参数中无fo参数时,则出错。 echo '变量\'fo\'的is_numeric为真,是数字'; }else{ echo '变量\'fo\'的is_numeric为假,不是数字'; } echo "<h2>\$_GET['fo']=''的情形:</h2>"; if($_GET['fo']==''){ //在参数中无fo参数时,则出错。 echo 'fo无值,空的字符串'; }elseif($_GET['fo']!=''){ echo 'fo有值,不为\'\'.'; } echo "<h2>\$_GET['sex']='m'的情形:</h2>"; if($_GET['sex']=='m'){ //当参数中无sex变量时就会出错。 echo '男的'; }elseif($_GET['sex']=='f'){ echo '女的'; } ?>
三、其他常用库函数
(1)ini_set ini_get——可操作配置参数列表
为了使自己的程序在不同的平台中拥有更好的兼容性,很多时候我们都要获取当前Php的运行环境参数。
比如我们常用到的:
获取 magic_quotes_gpc 状态,来决定当表单提交时我们是否转义(addslashes)数据;
设定 max_execution_time 来延长程序的执行时间;
设定 error_reporting 使自己的项目在开发与运营阶段切换;
设定 memory_limit 加大内存等等…
(2)ini_set(string varname, string newvalue ) : //设定环境配置的参数
ini_get(string varname) : //获取环境配置的参数
PHP ini_set函数是设置选项中的值,在执行函数后生效,脚本结束的时候,这个设置也失效。不是所有的选项都能被改函数设置的。具体那些值能够设置,可以查看手册中的列表
其实你把PHP ini_set函数和ini_get结合使的话,非常好。比如你想在配置文件里添加自己的包含文件路径,但是你有没有权限更改php.ini,那么你可以结合两个函数:
ini_set ( 'include_path' , ini_get ( 'include_path' ). ':/your_include_dir:' );
(3)chdir(dirname(FILE)); //切换到global.php所在目录
(4)ob_start(‘ui_handler');//设置输出缓冲区句柄为ui_handler,即系统首页面为ui_handler函数所定义的内容
(5)int intval(mixed var, int [base]);
本函数可将变量转成整数类型。可省略的参数 base 是转换的基底,默认值为 10。转换的变量 var 可以为数组或类之外的任何类型变量。
(6)error_reporting(report_level) 函数—— 设置 PHP 的报错级别并返回当前级别
其中report_level可取值为0、1、2、4、8、16、32、……、4096、8191
例子:任意数目的以上选项都可以用“或”来连接(用 OR 或 |),这样可以报告所有需要的各级别错误。例如,下面的代码关闭了用户自定义的错误和警告,执行了某些操作,然后恢复到原始的报错级别:
<?php //禁用错误报告 error_reporting(0); //报告运行时错误 error_reporting(E_ERROR | E_WARNING | E_PARSE); //报告所有错误 error_reporting(E_ALL); ?>
The above is the detailed content of Summary of common PHP functions and function libraries. For more information, please follow other related articles on the PHP Chinese website!