String manipulation of PHP introductory learning

Time:2022-9-16
Table of contents
  • Introduction to Strings
  • Difference between single and double quotes
  • string concatenator
  • Remove leading and trailing spaces and special characters from a string
    • 1. trim() function
    • 2. Itrim() function
    • 3. rtrim() function
  • Escape, restore string data
    • 1. Manually escape and restore string data
    • 2. Automatically escape and restore string data

Introduction to Strings

A string refers to a set consisting of zero or more characters. The characters mentioned here mainly include the following types:

  • Numeric types such as 1, 2, 3, etc.
  • Types of letters, such as a, b, c, d, etc.
  • Special characters such as #, $, %, ^, & etc.
  • Invisible characters such as \n (line feed), \r (carriage return), \t (Tab character), etc.

Among them, invisible characters are a special group of characters, which are used to control the formatted output of strings. They are not visible on the browser, and only the results of string output can be seen, as follows:

<!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>PHP language basics</title>

</head>
<body>

<?php
header("Content-Type:text/html;   charset=gb2312"); 
echo "pear\rapple\nbanan\tfruit";
?>

</body>
</html>

Running result: pear apple banan fruit

It is found that there is no line feed, carriage return, etc. on the browser. Click the webpage to view the source code:

Difference between single and double quotes

Strings usually take the whole of the string as the operation object, and generally use double quotes or single quotes to identify a string. There is a certain difference in the use of single and double quotes. There is no discernible difference between the two for normal strings defined. The difference between the two can be easily understood by working with variables.

The content in double quotes is parsed by PHP's parser, and any variable in double quotes will be converted to its value for output display; while the content of single quotes is &quot;what you see is what you get&quot;, whether there is or not Variables are output as normal strings.

<!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>PHP language basics</title>

</head>
<body>

<?php
header("Content-Type:text/html;   charset=gb2312");
$str="PHP";
$str1="$str";
$str2='$str';
echo $str1."<br>";
echo $str2;
?>

</body>
</html>

operation result:
PHP
$str

Tip: Single-quoted strings and double-quoted strings are handled differently in PHP. The contents of double-quoted strings can be interpreted and replaced, while the contents of single-quoted strings can only be treated as ordinary characters.
Note: All strings must be enclosed in single quotes before SQL queries to avoid possible injection vulnerabilities and SQL errors.

string concatenator

The half-width period &quot;.&quot; is a string concatenator, which can join two or more strings into one string. The connection of a large number of simple strings cannot be achieved by using the string concatenation symbol. PHP allows programmers to directly include string variables in double quotes. When double quotes (&quot;) are used after the echo statement, the following format can be used to achieve the same Effect.

<!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>PHP language basics</title>

</head>
<body>

<?php
header("Content-Type:text/html;   charset=gb2312");
$str1="Java";
$str2="PHP";
echo "$str1,$str2,C++";
?>

</body>
</html>

operation result:
Java,PHP,C++

Remove leading and trailing spaces and special characters from a string

When users enter data, they often enter extra spaces inadvertently. In some cases, spaces and special characters are not allowed in the string. In this case, the spaces and special characters in the string need to be removed. In PHP, the trim() function is provided to remove the spaces and special characters on the left and right sides of the string, the ltrim() function removes the spaces and special characters on the left side of the string, and the rtrim() function removes the spaces and special characters on the right side of the string.

1. trim() function

The trim() function is used to remove spaces at the beginning and end of a string, and returns the string with spaces removed.
The syntax format is as follows:


string trim(string str [,string charlist]);

The parameter str of the trim() function is the string object to be manipulated, and the parameter charlist is an optional parameter that specifies which characters need to be removed from the specified string. If this parameter is not set, all optional characters will be removed. .

The optional values ​​of the parameter charlist of the trim() function are shown in the following table.

Note: In addition to the default filter character list above, special characters to filter can also be provided in the charlist parameter.

Use the trim() function to remove spaces and special characters &quot;\r\r(: :)&quot; on the left and right sides of the string:

<!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>PHP language basics</title>

</head>
<body>

<?php
header("Content-Type:text/html;   charset=gb2312");
$str=&quot;\r\r(:@[email protected] Learn PHP @[email protected]:) &quot;;
echo trim($str);//Remove the spaces on the left and right sides of the string
echo "<br>";
echo trim($str,&quot;\r\r(: :)&quot;);//Remove the special characters on the left and right sides of the string\r\r(::)
?>

</body>
</html>

operation result:
(:@@Learn PHP @@:)
@@Learn PHP @@

2. Itrim() function

The Itrim() function is used to remove spaces from the left of a string or to specify a string.

The syntax format is as follows:


string ltrim( string str [,string charlist]);

Use the Itrim() function to remove spaces and special characters &quot;(:@[email protected]&quot; from the left of the string:

<!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>PHP language basics</title>

</head>
<body>

<?php
header("Content-Type:text/html;   charset=gb2312");
$str=&quot; (:@[email protected] Learn PHP @[email protected]:) &quot;;
echo trim($str);//Remove spaces on the left side of the string
echo "<br>";
echo ltrim($str,&quot; (:@[email protected]&quot;);//Remove the special characters on the left side of the string (:@[email protected]
?>

</body>
</html>

operation result:
(:@@Learn PHP @@:)
Learn PHP @[email protected] 🙂

3. rtrim() function

The rtrim() function is used to remove spaces from the right of a string.

The syntax format is as follows:


String rtrim(string str [,string charlist]);

Use the rtrim() function to remove spaces and special characters &quot;@[email protected]:)&quot; from the right of the string:

<!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>PHP language basics</title>

</head>
<body>

<?php
header("Content-Type:text/html;   charset=gb2312");
$str=&quot; (:@[email protected] Learn PHP @[email protected]:) &quot;;
echo trim($str);//Remove spaces on the right side of the string
echo "<br>";
echo rtrim($str,&quot; @[email protected]:)&quot;);//Remove the special characters on the right side of the string @[email protected]:)
?>

</body>
</html>

operation result:
(:@@Learn PHP @@:)
(:@[email protected] Learning PHP

Escape, restore string data

There are two ways to escape and restore strings: one is to manually escape and restore string data, and the other is to automatically escape and restore string data. These two methods are explained in detail below.

1. Manually escape and restore string data

Strings can be defined in three ways: single quotes ('), double quotes (&quot;&quot;), and delimiters ({}). And the easiest way to specify a simple string is to enclose it in single quotes ('). When using a string, it is very likely that there are characters in the string that are confused with the PHP script, so an escape statement must be done. This uses the escape character &quot;\&quot; in front of it.

&quot;\&quot; is an escape character, the first character immediately following &quot;\&quot; becomes meaningless or has special meaning. For example, ' is the delimiter of a string, and when it is written as \', the meaning of the delimiter is lost, and it becomes an ordinary single quotation mark'. The reader can output a single quote ' by echo '\'';, and the escape character &quot;\&quot; will not be displayed.

Tip 1: If you want to represent a single quote in a string, you need to escape it with a backslash (\). For example, to represent the string &quot;I'm&quot;, it needs to be written as &quot;I\'m&quot;.
Tip 2: For simple strings, it is recommended to use manual methods to escape strings, and for strings with a large amount of data, it is recommended to use automatic escape functions to escape strings.

Note: To manually escape strings, you can use the addcslashes() function to restore strings. The specific implementation method will be described below.

Use the escape character &quot;\&quot; to escape strings:

<?php
header("Content-Type:text/html;   charset=gb2312");
echo 'php,\'Learning PHP\'';
?>

operation result:
php, 'Learn PHP'

2. Automatically escape and restore string data

Automatic escaping and restoration of string data can be implemented by using the addslashes() and stripslashes() functions provided by PHP.

1.addslashes() function

The addslashes() function is used to add a slash &quot;\&quot; to the string str.
The syntax format is as follows:


string addslashes (string str)

2. stripslashes() function

The stripslashes() function is used to return the string str escaped using the addslashes() function as it is.


string stripslashes(string str);

Use the auto-escape character addslashes() function to escape the string, and then use the stripslashes() function to restore:

<!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>PHP language basics</title>

</head>
<body>

<?php
header("Content-Type:text/html;   charset=gb2312");
$str=&quot;php,'Learn PHP'&quot;;
echo $str."<br>";
$a=addslashes($str); //Escape the special characters in the string
echo $a."<br>";
$b=stripslashes($a);//Restore escape characters
echo $b;
?>

</body>
</html>

operation result:
php, 'Learn PHP'
php,\'Learn PHP\'
php, 'Learn PHP'

Tip: Before inserting all data into the database, it is necessary to use the addslashes() function to escape strings, so as to avoid errors when inserting into the database without escaping special characters. In addition, the automatically escaped strings implemented with the addslashes() function can be restored with the stripcslashes() function, but the data must be escaped again before inserting into the database.

The above two functions implement automatic escaping and restoration of the specified string. In addition to the methods described above, you can also limit the strings to be escaped and restored to a certain range, and use the addcslashes() and stripcslashes() functions to automatically escape and restore the strings within the specified range. The two functions are described in detail below.

3.addcslashes() function

Implement the characters in the escape string, that is, add a backslash before the specified character charlist.

The syntax format is as follows:


string addcslashes (string str, string charlist)

Parameter Description:
The parameter str is the string to be operated on, and the parameter charlist specifies which characters in the string are preceded by a backslash &quot;\&quot;. If the parameter charlist contains words such as \n, \r, etc.
characters, will be converted in C language style, and other non-alphanumeric characters with ASCII codes lower than 32 and higher than 126 are converted to octal representation.

Note: When defining the range of the parameter charlist, you need to specify the characters in the range at the beginning and end.

4. stripcslashes() function

The stripcslashes() function is used to restore the string str escaped by the addcslashes() function.

The syntax format is as follows:


string stripcslashes (string str)

Use the addcslashes() function to escape the string, and use the stripcslashes() function to restore the escaped string.

<!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>PHP language basics</title>

</head>
<body>

<?php
header("Content-Type:text/html;   charset=gb2312");
$str=&quot;Learn PHP&quot;;
echo $str."<br>";
$b=addcslashes($str,&quot;Learn PHP&quot;);
echo $b."<br>";
$c=stripcslashes($b);
echo $c;
?>

</body>
</html>

operation result:
Learn PHP
\321\247\317\260\P\H\P
Learn PHP

Tip: In the cache file, generally use the addcslashes() function to escape the value of the cached data in the specified range.

The above is the detailed content of string manipulation for PHP entry learning. For more information on PHP string manipulation, please pay attention to other related articles on developpaer!