PHP之主要函数
PHP之重要函数
积少成多——作者:zccst
8 , string base64_encode(string data);
本函数将字符串以 MIME BASE64 编码。此编码方式可以让中文字或者图片也能在网络上顺利传输。在 BASE64 编码后的字符串只包含英文字母大小写、阿拉伯数字、加号与反斜线,共 64 个基本字符,不包含其它特殊的字符,因而才取名 BASE64。编码后的字符串比原来的字符串长度再加 1/3 左右。更多的 BASE64 编码信息可以参考 RFC2045 文件之 6.8 节。
7,similar_text
similar_text() 函数计算两个字符串的匹配字符的数目,也可以计算两个字符串的相似度(以百分比计)。
参数 描述
string1 必需。规定要比较的第一个字符串。
string2 必需。规定要比较的第二个字符串。
percent 可选。规定供存储百分比相似度的变量名。
similar_text("Hello World","Hello Peter",$percent);echo $percent;
输出:63.6363636364
6,spl_autoload_register('autoloader');
function autoloader($className){ if(file_exists(dirname(__FILE__)."/$className.php")){ include dirname(__FILE__)."/$className.php"; if(class_exists($className,false)){ return true; } } return false;}
批注:
bool class_exists ( string $class_name [, bool $autoload = true ] )
第二个参数:Whether or not to call __autoload by default.
只有当第二个参数为false时,才能不开启autoload,以节省开支。
__autoload机制:
参阅另一篇文章:php autoload机制。
5,addslashes()和stripslashes()
addslashes() 函数在指定的预定义字符前添加反斜杠。 这些预定义字符是: 单引号 (') 双引号 (") 反斜杠 (\) NULL
$str = "Is your name O'reilly?";// Outputs: Is your name O\'reilly?echo addslashes($str);
stripslashes() 函数删除由 addslashes() 函数添加的反斜杠
4,get_magic_quotes_gpc()
int get_magic_quotes_gpc ( void )
Returns the current configuration setting of magic_quotes_gpc
Keep in mind that attempting to set magic_quotes_gpc at runtime will not work.
For more information about magic_quotes, see this security section.
批注:取得 PHP 环境变量 magic_quotes_gpc(GPC, Get/Post/Cookie) 的值。返回 0 表示关闭本功能;返回 1 表示本功能打开。
当 magic_quotes_gpc 打开时,所有的 ' (单引号), " (双引号), \ (反斜线) and 空字符会自动转为含有反斜线的转义字符。
当magic_quotes_gpc = On时,系统会自动处理单引号等问题,用不用addslashes()和stripslashes()都没关系,但是如果添加数据时用了addslashes(),那么显示数据时必须要stripslashes()
当magic_quotes_gpc = Off时,系统不会处理单引号等问题,所以插入数据时必须要使用addslashes(),显示数据时则不需要使用stripslashes()。
既然有了分析,做程序时要怎么办呢?根据以上两种情况,可得:
不管magic_quotes_gpc是On还是Off,咱添加数据时都用addslashes(),当On时,必须使用stripslashes(),Off时则不能用stripslashes()。
如何判断On还是Off呢?用get_magic_quotes_gpc()。
最后举例:
//1,存入数据库$Content=addslashes(”这里面是数据,不管有没单引号或者还是变量”);//插入数据到数据库,代码省略//2,从数据库取$Content=”从数据库读取的数据”;if(get_magic_quotes_gpc()){ $Content=stripslashes($Content); }echo $Content;/************ 例子2 **************/echo get_magic_quotes_gpc(); // 1echo $_POST['lastname']; // O\'reillyecho addslashes($_POST['lastname']); // O\\\'reillyif (get_magic_quotes_gpc()) { $lastname = stripslashes($_POST['lastname']);}else { $lastname = $_POST['lastname'];}// If using MySQL$lastname = mysql_real_escape_string($lastname);echo $lastname; // O\'reilly$sql = "INSERT INTO lastnames (lastname) VALUES ('$lastname')";
3,get_file_contents($fileName)
把文件读入一个字符串
file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法。
file_put_contents(string $filename , string $data [, int $flags [, resource $context ]] )
将一个字符串写入文件
2,str_replace(array('\r\n'), array("\n"), $str)
1,strip_tags($v)
原型:string strip_tags ( string $str [, string $allowable_tags ] )
解读:This function tries to return a string with all NUL bytes, HTML and PHP tags stripped from a given str. It uses the same tag stripping state machine as the fgetss() function.
举例:
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';echo strip_tags($text);echo "\n";// Allow <p> and <a>echo strip_tags($text, '<p><a>');
输出:
Test paragraph. Other text
Test paragraph.
Other text
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
