Main functions of PHP

Source: Internet
Author: User
Tags autoload autoloader first string
Important Functions of PHP
Added up--Author: zccst

8, String Base64_encode (string data);
This function encodes a string in MIME BASE64. This encoding allows Chinese text or pictures to be transferred smoothly on the network. The BASE64 encoded string contains only the English letter case, Arabic numerals, plus and backslash, a total of 64 basic characters, does not contain other special characters, so it is named BASE64. The encoded string is 1/3 or so longer than the original string length. More information on BASE64 encoding can be found in section 6.8 of the RFC2045 file.

7,similar_text
The Similar_text () function calculates the number of matching characters for two strings, or calculates the similarity of two strings (in percent).
Parameter description
String1 required. Specifies the first string to compare.
string2 required. Specifies a second string to compare.
Percent is optional. A variable name that specifies the similarity of the storage percentage.
Similar_text ("Hello World", "Hello Peter", $percent); Echo $percent;

Output: 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;}

Comments:
BOOL Class_exists (String $class _name [, bool $autoload = True])
Second parameter: Whether or not to call __autoload by default.
You can save money by not turning on AutoLoad only if the second argument is false.
__autoload mechanism:
See another article: PHP autoload mechanism.


5,addslashes () and stripslashes ()
The Addslashes () function adds a backslash before the specified predefined character. These predefined characters are: Single quotation mark (') Double quotation mark (") backslash (\) NULL
$str = "Is your name O ' Reilly?"; /Outputs:is Your name o\ ' Reilly?echo addslashes ($STR);

The Stripslashes () function removes backslashes added by the addslashes () function

4,GET_MAGIC_QUOTES_GPC ()
int get_magic_quotes_gpc (void)
Returns the current configuration setting of MAGIC_QUOTES_GPC
Keep in mind, attempting to set MAGIC_QUOTES_GPC at runtime won't work.
For more information about Magic_quotes, see this security section.
Annotations: Gets the value of the PHP environment variable MAGIC_QUOTES_GPC (GPC, Get/post/cookie). Returns 0 to turn off this function; return 1 indicates that this function is turned on.
When MAGIC_QUOTES_GPC is turned on, all the ' (single quotes), ' (double quotes), \ (backslash) and null characters are automatically converted to the escape character that contains the backslash.


When MAGIC_QUOTES_GPC = ON, the system automatically handles issues such as single quotes, with no addslashes () and stripslashes (), but if the data is added with Addslashes (), Then the data must be displayed stripslashes ()

When MAGIC_QUOTES_GPC = off, the system does not handle problems such as single quotes, so you must use Addslashes () when inserting data, and you do not need to use stripslashes () when displaying data.

Since there is analysis, what to do when the program? Depending on the above two conditions, it is possible to:

Regardless of whether MAGIC_QUOTES_GPC is on or off, we use addslashes () when we add data, and when on, we must use Stripslashes (), which is not stripslashes () when off.

How do I tell if it is on or off? Use GET_MAGIC_QUOTES_GPC ().

The last example:
1, into the database $content=addslashes ("This is the data, whether there is no single quotation mark or variable");//Insert data into the database, the code omits//2, from the database $content= "data read from the database"; if (get_magic _QUOTES_GPC ()) {  $Content =stripslashes ($Content);} echo $Content;/************  Example 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)
To read a file into a string
The file_get_contents () function is the preferred method for reading the contents of a file into a string.

File_put_contents (String $filename, String $data [, int $flags [, Resource $context]])
Writes a string to a file

2,str_replace (Array (' \ r \ n '), array ("\ n"), $STR)


1,strip_tags ($v)
Prototype: String Strip_tags (String $str [, String $allowable _tags])

Interpretation: 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.

Example:
$text = '

Test paragraph.

and Echo strip_tags ($text, '

');


Output:
Test paragraph. Other text

Test paragraph.

Other text



  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.