<ul id="xaw99"></ul>
  • <rt id="xaw99"><small id="xaw99"></small></rt>
      <i id="xaw99"></i>

      \n\n$str = \"WebServer:<\/B> & 'Linux' & 'Apache'\"; \/\/String with HTML tags and single quotes
      \necho htmlspecialchars($str,ENT_COMPAT); \/\/Convert HTML tags and convert double quotes
      \necho \"
      n\";
      \necho htmlspecialchars($str,ENT_QUOTES); \/\/Convert HTML tags and convert two kinds of quotes
      \necho \"
      n\";
      \necho htmlspecialchars($str,ENT_NOQUOTES); \/\/Convert HTML tags and unquote conversion
      \necho \"
      n\";
      \n?>
      \n<\/body>
      \n<\/html>
      \n<\/div>\n

      Output results in browser
      \n<\/p>\n

      \nCopy code<\/u><\/span> The code is as follows:<\/div>\n
      \n
      \nWebServer:<\/B> & ‘Linux’ & ‘Apache’
      \nWebServer:<\/B> & ‘Linux’ & ‘Apache’
      \nWebServer:<\/B> & ‘Linux’ & ‘Apache’
      \n<\/div>\n
      \nIf you view the source code in a browser, you will see the following result:
      \n
      \nCopy code<\/u><\/span> The code is as follows:<\/div>\n
      \n
      \n
      \n

      国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂


      \nWebServer:<\/B>&'Linux'&'Apache'
      \/\/Single quotes are not converted
      \nWebServer:<\/B>&'Linux'&'Apache'

      \nWebServer:<\/B>&'Linux'&'Apache' \/\/Single quotes are not converted
      \n<\/body>
      \n<\/html>
      \n<\/div>\n

      The htmlentities() function is also provided in PHP, which can convert all non-ASCII characters into corresponding entity codes. This function has the same syntax format as the htmlspecialchars() function, which can escape more HTML characters. The following code is an example of using the htmlentities() function:
      \n<\/p>\n

      \nCopy code<\/u><\/span> The code is as follows:<\/div>\n
      \n
      \n\n$str = \"A 'quote' isbold<\/b>\";
      \n\/\/Output&0qrave;??? 'quote' ê? <:b>bold<\/b>
      \necho htmlentities($str);
      \n\/\/Output: a 'quote' is bold<\/b>
      \necho htmlentities($str,ENT_QUOTES,gb2312);
      \n?>
      \n<\/div>\n

      When processing the data submitted in the form, not only the HTML markup symbols and some special characters must be converted into HTML entities through the functions introduced earlier, but the quotation marks must also be processed. Because characters such as \"'\", \"\"\" and \"\" in the submitted form data will be automatically added with a slash \"\". This is due to the option magic_quotes_gpc in the PHP configuration file php.ini. The default is If it is turned on, use the function stripslashes() to remove the backslash. If it is not processed, the database may mistake it as a control symbol and cause an error. There is only one function stripslashes(). The processed string is used as a parameter and the processed string is returned. The htmlspecialchars() function and the stripslashes() function are usually used to jointly process the data submitted in the form. <\/p>\n

      The function of the function stripslashes() is to remove the backslash \"\". If there are two consecutive backslashes, only one will be removed. Corresponding to it is another function addslashes(). As the function name implies, it will add necessary backslashes before \"'\", \"\"\", \"\" and NULL characters. <\/p>\n

      The function htmlspecialchars() converts the mark symbols in the function HTML into the corresponding HTML entities. Sometimes it is also necessary to directly delete the HTML tags input by the user. The strip_tags() function provided in PHP can delete all HTML tags in the string by default, and can also selectively delete some HTML tags. Such as bulletin boards or guest message boards, it is quite necessary to have applications in this area. For example, when users publish articles in a forum, they can reserve some HTML tags that can change font size, color, bold and italics, etc., and delete some HTML tags that have an impact on page layout. The prototype of the function strip_tags() is as follows:
      \n<\/p>\n

      \nCopy code<\/u><\/span> The code is as follows:<\/div>\n
      \n
      \nstring strip_tags(string str[,string allowable_tags]); \/\/Delete HTML tag function
      \n<\/div>\n
      \nThis function has two parameters. The first parameter provides the string to be processed. The second parameter is an optional list of HTML tags. The HTML tags placed in the list will be retained, and all others will be deleted. . All HTML tags are deleted by default. The following program uses the scope of this function as follows:
      \n
      \nCopy code<\/u><\/span> The code is as follows:<\/div>\n
      \n
      \n\n$str = \"Linux<\/font> Apache<\/i> Mysql<\/u> PHP<\/b>\";
      \necho strip_tags($str); \/\/All HTML tags are deleted, output: Linux Apache Mysql PHP
      \necho strip_tags($str,\"\"); \/\/OutputLinux<\/font>Apache Mysql PHP
      \necho strip_tags($str,\"\"); \/\/Output Linux Apache<\/i> Mysql<\/u> PHP< \/b>
      \n?>
      \n<\/div>\n

      4. Other string formatting functions<\/strong><\/p>\n

      There are many string formatting functions. As long as you want to get the string that needs to be formatted, you can call the system functions provided in PHP. You rarely need to define your own string formatting function. <\/p>\n

      ①Function strrev()<\/strong><\/p>\n

      The function of this function is to reverse the input string, provide only a string to be processed as a parameter, and return the reversed string. As shown below:
      \n<\/p>\n

      \nCopy code<\/u><\/span> The code is as follows:<\/div>\n
      \n
      \n\necho strrev(\"http:\/\/www.lampbrother.net\"); \/\/Output after reverse: ten.rehtorbpmal.www\/\/:ptth
      \n?>
      \n<\/div>\n

      ②Function number_format()<\/strong><\/p>\n

      The number_format() function formats numbers by thousands grouping. The function looks like this:
      \n<\/p>\n

      \nCopy code<\/u><\/span> The code is as follows:<\/div>\n
      \n
      \nstring number_format(float number[,int decimals[,string dec_point,string thousands_sep]])
      \n<\/div>\n

      <\/p>\n

      \nCopy code<\/u><\/span> The code is as follows:<\/div>\n
      \n
      \n\n$number = 123456789;
      \necho number_format($number); \/\/Output: 123,456,789 thousand-digit separated string
      \necho number_format($number,2); \/\/Output: 123,456,789.00 with two decimal places after the decimal point
      \necho number_format($number,2,\",\",\".\"); \/\/Output 123.456.789, the thousand digits are separated by (.) and retain two decimal places
      \n?>
      \n<\/div>\n

      ③Function md5()<\/strong><\/p>\n

      With the popularity of the Internet, hacker attacks have become a concern for network administrators. Statistics show that 70% of attacks come from within, so corresponding preventive measures must be taken to curb attacks within the system. The importance of preventing insider attacks also lies in the fact that insiders have a good understanding of where data is stored and the importance of the information, which makes insider attacks more likely to be effective. Attackers steal the identity information of legitimate users and communicate with others under fake identities. Therefore, when a user registers, the password should be encrypted before being added to the database. This can prevent internal attackers from directly querying the authorization table in the database and stealing the identity information of legitimate users. <\/p>\n

      The md5() function is to encrypt a string with the MD5 algorithm and returns a 32-bit hexadecimal string by default. <\/p>\n

      <\/p>\n

      \nCopy code<\/u><\/span> The code is as follows:<\/div>\n
      \n
      \n\n$password = \"lampbrother\";
      \necho md5($password).\"
      \";
      \n
      \n\/\/Match the entered password with the one saved in the database
      \nif(md5($password) == '5f1ba7d4b4bf96fb8e7ae52fc6297aee'){
      \necho \"The passwords are consistent and the login is successful\";
      \n}
      \n?>
      \n<\/div>\n

      In PHP, a function md5_file() is provided for MD5 encryption of files. The method of use is similar to the md5() function.
      \n<\/p>\n\n\n\n\n\n\n\n

      <\/p>\n

      \nhttp:\/\/www.bkjia.com\/PHPjc\/914060.html<\/span>www.bkjia.com<\/span>true<\/span>http: \/\/www.bkjia.com\/PHPjc\/914060.html<\/span>TechArticle<\/span>A summary of commonly used string formatting functions in PHP. The formatting of PHP function strings is to process the string as a specific format. Usually the data submitted by the user to the server from the form is...<\/span>\n<\/div>\n
      <\/div>"}
      Table of Contents
      A summary of commonly used string formatting functions in PHP, php functions
      Home Backend Development PHP Tutorial Summary of commonly used string formatting functions in PHP, php functions_PHP tutorial

      Summary of commonly used string formatting functions in PHP, php functions_PHP tutorial

      Jul 13, 2016 am 10:13 AM
      php function string format

      A summary of commonly used string formatting functions in PHP, php functions

      String formatting is to process the string into a specific format. Usually the data submitted by users to the server from the form is in the form of strings. In order to achieve the desired output effect, these strings need to be processed in a certain format before use. The commonly seen string formatting functions are as shown below:

      Note: Most of the strings processed by the string functions provided in PHP do not modify the original string, but return a formatted new string.

      1. Remove spaces and string padding functions

      A space is also a valid character and occupies a position in the string. When users enter data in a form, they often unintentionally enter extra meaningless spaces. Therefore, when the PHP script receives the data processed through the form, the first thing it processes is the extra spaces in the string, or other meaningless symbols. This work can be done in PHP through the ltrim(), rtrim() and trim() functions. The syntax format of these three functions is the same, but their functions are different. Their syntax format is as follows:

      Copy code The code is as follows:

      string ltrim(string str[,string charlist]) //Remove spaces or other predefined characters from the left side of the string
      string rtrim(string str[,string charlist]) //Remove blank characters or other predefined characters from the right side of the string
      string trim(string str[,string charlist]) //Remove blank characters or other predefined characters from both ends of the string

      These three functions are used to remove whitespace characters or other predefined characters from the left, right, and both ends of a string respectively. The processed results will be returned in the form of new strings and will not be modified on the original strings. The first parameter str is the string to be processed and is required. The second parameter charlist is a filter string, used to specify the special symbols you want to remove. This parameter is optional. If you do not specify a filter string, the following characters will be removed by default.

      ★"":space
      ★”0”:NULL
      ★"t":tab
      ★”n”:new line
      ★"r": Enter

      In addition, you can also use the ".." symbol to specify a range that needs to be removed, such as "0..9" or "a..z" to remove numbers and small letters in the ASCII code value. Their usage code is as follows:

      Copy code The code is as follows:

      $str = "123 This is a test ..."; //Declare a test string, starting with a number on the left and an ellipsis
      on the right echo ltrim($str,"0..9"); //Filter out the numbers on the left side of the string and output This is a test...
      echo rtrim($str,".") //Filter out all "." on the right side of the string, output: 123 This is a test
      echo trim ($str, "0..9 A..Z ."); //Filter out the numbers, uppercase letters and "." at both ends of the string, output: his is a test
      ?>

      Not only can you filter out the content in the string as needed, you can also use the str_pad() function to fill the string as needed. It can be used to protect some sensitive information, such as data pairing and arrangement. The prototype of its function is as follows:

      Copy code The code is as follows:

      string str_pad(string input,int pad_length[,string pad_string[,int pad_type]])

      This function has 4 parameters, the first parameter specifies the string to be processed. The second parameter gives the length of the processed string. If the value is less than the length of the original string, no operation is performed. The third parameter specifies the string used for padding. It is an optional parameter. If not specified, spaces will be used for padding by default. The last parameter specifies the direction of padding, which has three optional values: STR_PAD_BOTH, STR_PAD_LEFT and STR_PAD_RIGHT, which represent padding at both ends, left and right of the string respectively. Also an optional parameter, if not specified, the default value is STR_PAD_RIGHT. The usage code of function str_pad() is as follows:
      Copy code The code is as follows:

      $str = "LAMP";
      echo str_pad($str,10); //The specified length is 10, and by default, spaces are used to fill "LAMP" on the right
      echo str_pad($str,10,"-="STR_PAD_LEFT); //Specify the length as 10, and specify "-=-=-=LAMP" to be padded on the left
      echo str_pad($str,10,"_"STR_PAD_BOTH); //Specify the length as 10, and specify "___LAMP___" to be padded on the left
      ?>

      2. String case conversion

      There are 4 string case conversion functions provided in PHP. They all have only one optional parameter, which is to pass in the string to be converted. You can use these functions directly to complete the case conversion operation. The function strtoupper() is used to convert all the given strings into uppercase letters; the function strtolower() is used to convert all the given strings into lowercase letters; the function ucfirst() is used to convert all the characters in the given string The first letter is converted to uppercase, while the remaining characters remain unchanged; the function ucwords() is used to convert the first letter of all words separated by spaces in the given string to uppercase. The following program is the usage code of these functions, as shown below:

      Copy code The code is as follows:

      $lamp = "lamp is composed of Linux, Apache, MySQL and PHP";
      echo strtolower($lamp); //Output: lamp is composed of linux, apache, mysql and php
      echo strtoupper($lamp); //Output: LAMP IS CONPOSED OF LINUX, APACHE, MYSQL AND PHP
      echo ucfirst($lamp); //Output: Lamp is composed of Linux, Apache, MySQL and PHP
      echo ucwords($lamp); //Output: Lamp Is Composed Of Linux, Apache, MySQL And PHP
      ?>

      These functions only work as described in their descriptions. To ensure that the first letter of a string is an uppercase letter and the rest are lowercase letters, you need to use a matching method. As shown below:
      Copy code The code is as follows:

      $lamp = "lamp is composed of Linux, Apache, MySQL and PHP";
      echo ucfirst(strtolower($lamp)); //Output: Lamp is composed of linux, apache, mysql and php
      ?>

      3. String formatting related to HTML tags

      HTML input forms and resources attached to URLs are ways for users to submit data to the server. If not handled well, they may become entry points for hackers to attack the server. For example, when a user publishes an article, if the article contains some HTML formatting tags or JavaScript page redirection code, the layout of the page will definitely change if it is directly output and displayed. Because these codes are sent to the browser, the browser interprets them as valid codes. Therefore, in PHP scripts, the data content submitted by the user must be processed first. PHP provides us with a very comprehensive range of HTML-related string formatting functions, which can effectively control the output of HTML text.

      ①Function nl2br()

      The string "
      " output in the browser marks a newline, and many people are accustomed to using "n" as the newline symbol, but the browser does not recognize the newline character of this string. Even if there are multiple lines of text, only this line will be displayed in the browser. The nl2br() function inserts the HTML newline character "
      " before each new line "n" in the string. The usage of this function is as follows:

      Copy code The code is as follows:

      echo nl2br("One line.nAnother line."); //Add "
      " mark before "n"
      /*Output the following two lines of results
      One line.

      Another line.
      */
      ?>

      ②Function htmlspecialchars()

      If you do not want the browser to parse HTML tags directly, you need to convert the special characters in the HTML tags into HTML entities. For example, convert "<" to "<" and ">" to ">". In this way, the HTML tag browser will not parse it, but will output the HTML text in the browser as it is. The htmlspecialchars() function provided in PHP can convert some predefined strings into HTML entities. This function is used to prevent user-supplied text from containing HTML tags, such as bulletin boards or guest message boards. The following characters can be converted by this function:

      ★ "&" (ampere) is converted to "&".
      ★""" (double quotation mark) is converted to """.
      ★"'" (single quote) is converted to "'".
      ★"<" (less than) is converted to "<".
      ★">" (greater than) is converted to ">".

      The prototype of this function is as follows:

      Copy code The code is as follows:

      string htmlspecialchars(string string [,int quote_style[,string charset]])

      The first parameter in this function is a string with HTML tags to be processed. The second parameter is used to determine how quotation marks are converted. The default value is ENT_COMPAT, which will only convert double quotes and retain single quotes; ENT_QUOTES, which will convert both types of quotes; and ENT_NOQUOTES, which will not convert the quotes. The third parameter is used to specify the character set of the string being processed. The default character set is "ISO88511-1".

      Copy code The code is as follows:



      $str = "WebServer: & 'Linux' & 'Apache'"; //String with HTML tags and single quotes
      echo htmlspecialchars($str,ENT_COMPAT); //Convert HTML tags and convert double quotes
      echo "
      n";
      echo htmlspecialchars($str,ENT_QUOTES); //Convert HTML tags and convert two kinds of quotes
      echo "
      n";
      echo htmlspecialchars($str,ENT_NOQUOTES); //Convert HTML tags and unquote conversion
      echo "
      n";
      ?>


      Output results in browser

      Copy code The code is as follows:

      WebServer: & ‘Linux’ & ‘Apache’
      WebServer: & ‘Linux’ & ‘Apache’
      WebServer: & ‘Linux’ & ‘Apache’

      If you view the source code in a browser, you will see the following result:
      Copy code The code is as follows:



      WebServer:&'Linux'&'Apache'
      //Single quotes are not converted
      WebServer:&'Linux'&'Apache'

      WebServer:&'Linux'&'Apache' //Single quotes are not converted


      The htmlentities() function is also provided in PHP, which can convert all non-ASCII characters into corresponding entity codes. This function has the same syntax format as the htmlspecialchars() function, which can escape more HTML characters. The following code is an example of using the htmlentities() function:

      Copy code The code is as follows:

      $str = "A 'quote' isbold";
      //Output&0qrave;??? 'quote' ê? <:b>bold
      echo htmlentities($str);
      //Output: a 'quote' is bold
      echo htmlentities($str,ENT_QUOTES,gb2312);
      ?>

      When processing the data submitted in the form, not only the HTML markup symbols and some special characters must be converted into HTML entities through the functions introduced earlier, but the quotation marks must also be processed. Because characters such as "'", """ and "" in the submitted form data will be automatically added with a slash "". This is due to the option magic_quotes_gpc in the PHP configuration file php.ini. The default is If it is turned on, use the function stripslashes() to remove the backslash. If it is not processed, the database may mistake it as a control symbol and cause an error. There is only one function stripslashes(). The processed string is used as a parameter and the processed string is returned. The htmlspecialchars() function and the stripslashes() function are usually used to jointly process the data submitted in the form.

      The function of the function stripslashes() is to remove the backslash "". If there are two consecutive backslashes, only one will be removed. Corresponding to it is another function addslashes(). As the function name implies, it will add necessary backslashes before "'", """, "" and NULL characters.

      The function htmlspecialchars() converts the mark symbols in the function HTML into the corresponding HTML entities. Sometimes it is also necessary to directly delete the HTML tags input by the user. The strip_tags() function provided in PHP can delete all HTML tags in the string by default, and can also selectively delete some HTML tags. Such as bulletin boards or guest message boards, it is quite necessary to have applications in this area. For example, when users publish articles in a forum, they can reserve some HTML tags that can change font size, color, bold and italics, etc., and delete some HTML tags that have an impact on page layout. The prototype of the function strip_tags() is as follows:

      Copy code The code is as follows:

      string strip_tags(string str[,string allowable_tags]); //Delete HTML tag function

      This function has two parameters. The first parameter provides the string to be processed. The second parameter is an optional list of HTML tags. The HTML tags placed in the list will be retained, and all others will be deleted. . All HTML tags are deleted by default. The following program uses the scope of this function as follows:
      Copy code The code is as follows:

      $str = "Linux Apache Mysql PHP";
      echo strip_tags($str); //All HTML tags are deleted, output: Linux Apache Mysql PHP
      echo strip_tags($str,""); //OutputLinuxApache Mysql PHP
      echo strip_tags($str,""); //Output Linux Apache Mysql PHP< /b>
      ?>

      4. Other string formatting functions

      There are many string formatting functions. As long as you want to get the string that needs to be formatted, you can call the system functions provided in PHP. You rarely need to define your own string formatting function.

      ①Function strrev()

      The function of this function is to reverse the input string, provide only a string to be processed as a parameter, and return the reversed string. As shown below:

      Copy code The code is as follows:

      echo strrev("http://www.lampbrother.net"); //Output after reverse: ten.rehtorbpmal.www//:ptth
      ?>

      ②Function number_format()

      The number_format() function formats numbers by thousands grouping. The function looks like this:

      Copy code The code is as follows:

      string number_format(float number[,int decimals[,string dec_point,string thousands_sep]])

      Copy code The code is as follows:

      $number = 123456789;
      echo number_format($number); //Output: 123,456,789 thousand-digit separated string
      echo number_format($number,2); //Output: 123,456,789.00 with two decimal places after the decimal point
      echo number_format($number,2,",","."); //Output 123.456.789, the thousand digits are separated by (.) and retain two decimal places
      ?>

      ③Function md5()

      With the popularity of the Internet, hacker attacks have become a concern for network administrators. Statistics show that 70% of attacks come from within, so corresponding preventive measures must be taken to curb attacks within the system. The importance of preventing insider attacks also lies in the fact that insiders have a good understanding of where data is stored and the importance of the information, which makes insider attacks more likely to be effective. Attackers steal the identity information of legitimate users and communicate with others under fake identities. Therefore, when a user registers, the password should be encrypted before being added to the database. This can prevent internal attackers from directly querying the authorization table in the database and stealing the identity information of legitimate users.

      The md5() function is to encrypt a string with the MD5 algorithm and returns a 32-bit hexadecimal string by default.

      Copy code The code is as follows:

      $password = "lampbrother";
      echo md5($password)."
      ";

      //Match the entered password with the one saved in the database
      if(md5($password) == '5f1ba7d4b4bf96fb8e7ae52fc6297aee'){
      echo "The passwords are consistent and the login is successful";
      }
      ?>

      In PHP, a function md5_file() is provided for MD5 encryption of files. The method of use is similar to the md5() function.

      www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/914060.htmlTechArticleA summary of commonly used string formatting functions in PHP. The formatting of PHP function strings is to process the string as a specific format. Usually the data submitted by the user to the server from the form is...
      Statement of this Website
      The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

      Hot AI Tools

      Undress AI Tool

      Undress AI Tool

      Undress images for free

      Undresser.AI Undress

      Undresser.AI Undress

      AI-powered app for creating realistic nude photos

      AI Clothes Remover

      AI Clothes Remover

      Online AI tool for removing clothes from photos.

      Clothoff.io

      Clothoff.io

      AI clothes remover

      Video Face Swap

      Video Face Swap

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

      Hot Tools

      Notepad++7.3.1

      Notepad++7.3.1

      Easy-to-use and free code editor

      SublimeText3 Chinese version

      SublimeText3 Chinese version

      Chinese version, very easy to use

      Zend Studio 13.0.1

      Zend Studio 13.0.1

      Powerful PHP integrated development environment

      Dreamweaver CS6

      Dreamweaver CS6

      Visual web development tools

      SublimeText3 Mac version

      SublimeText3 Mac version

      God-level code editing software (SublimeText3)

      How to access a character in a string by index in PHP How to access a character in a string by index in PHP Jul 12, 2025 am 03:15 AM

      In PHP, you can use square brackets or curly braces to obtain string specific index characters, but square brackets are recommended; the index starts from 0, and the access outside the range returns a null value and cannot be assigned a value; mb_substr is required to handle multi-byte characters. For example: $str="hello";echo$str[0]; output h; and Chinese characters such as mb_substr($str,1,1) need to obtain the correct result; in actual applications, the length of the string should be checked before looping, dynamic strings need to be verified for validity, and multilingual projects recommend using multi-byte security functions uniformly.

      How Do Generators Work in PHP? How Do Generators Work in PHP? Jul 11, 2025 am 03:12 AM

      AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

      How to prevent session hijacking in PHP? How to prevent session hijacking in PHP? Jul 11, 2025 am 03:15 AM

      To prevent session hijacking in PHP, the following measures need to be taken: 1. Use HTTPS to encrypt the transmission and set session.cookie_secure=1 in php.ini; 2. Set the security cookie attributes, including httponly, secure and samesite; 3. Call session_regenerate_id(true) when the user logs in or permissions change to change to change the SessionID; 4. Limit the Session life cycle, reasonably configure gc_maxlifetime and record the user's activity time; 5. Prohibit exposing the SessionID to the URL, and set session.use_only

      How to URL encode a string in PHP with urlencode How to URL encode a string in PHP with urlencode Jul 11, 2025 am 03:22 AM

      The urlencode() function is used to encode strings into URL-safe formats, where non-alphanumeric characters (except -, _, and .) are replaced with a percent sign followed by a two-digit hexadecimal number. For example, spaces are converted to signs, exclamation marks are converted to!, and Chinese characters are converted to their UTF-8 encoding form. When using, only the parameter values ??should be encoded, not the entire URL, to avoid damaging the URL structure. For other parts of the URL, such as path segments, the rawurlencode() function should be used, which converts the space to . When processing array parameters, you can use http_build_query() to automatically encode, or manually call urlencode() on each value to ensure safe transfer of data. just

      PHP get the first N characters of a string PHP get the first N characters of a string Jul 11, 2025 am 03:17 AM

      You can use substr() or mb_substr() to get the first N characters in PHP. The specific steps are as follows: 1. Use substr($string,0,N) to intercept the first N characters, which is suitable for ASCII characters and is simple and efficient; 2. When processing multi-byte characters (such as Chinese), mb_substr($string,0,N,'UTF-8'), and ensure that mbstring extension is enabled; 3. If the string contains HTML or whitespace characters, you should first use strip_tags() to remove the tags and trim() to clean the spaces, and then intercept them to ensure the results are clean.

      PHP get the last N characters of a string PHP get the last N characters of a string Jul 11, 2025 am 03:17 AM

      There are two main ways to get the last N characters of a string in PHP: 1. Use the substr() function to intercept through the negative starting position, which is suitable for single-byte characters; 2. Use the mb_substr() function to support multilingual and UTF-8 encoding to avoid truncating non-English characters; 3. Optionally determine whether the string length is sufficient to handle boundary situations; 4. It is not recommended to use strrev() substr() combination method because it is not safe and inefficient for multi-byte characters.

      How to set and get session variables in PHP? How to set and get session variables in PHP? Jul 12, 2025 am 03:10 AM

      To set and get session variables in PHP, you must first always call session_start() at the top of the script to start the session. 1. When setting session variables, use $_SESSION hyperglobal array to assign values ??to specific keys, such as $_SESSION['username']='john_doe'; it can store strings, numbers, arrays and even objects, but avoid storing too much data to avoid affecting performance. 2. When obtaining session variables, you need to call session_start() first, and then access the $_SESSION array through the key, such as echo$_SESSION['username']; it is recommended to use isset() to check whether the variable exists to avoid errors

      How to prevent SQL injection in PHP How to prevent SQL injection in PHP Jul 12, 2025 am 03:02 AM

      Key methods to prevent SQL injection in PHP include: 1. Use preprocessing statements (such as PDO or MySQLi) to separate SQL code and data; 2. Turn off simulated preprocessing mode to ensure true preprocessing; 3. Filter and verify user input, such as using is_numeric() and filter_var(); 4. Avoid directly splicing SQL strings and use parameter binding instead; 5. Turn off error display in the production environment and record error logs. These measures comprehensively prevent the risk of SQL injection from mechanisms and details.

      See all articles