The string is a sequence of characters, such as "Hello world!".
PHP String Functions
In this section, we will learn about commonly used string manipulation functions.
PHP strlen () function
The strlen () function returns the length of a string, measured in characters.
The following example returns the length of the string "Hello world!":
Instance
<? PHP Echo strlen ("Hello world!" );? >
Running an instance
The output of the above code is: 12
Tip: strlen () is commonly used in loops and other functions when it is important to determine when a string ends. (for example, in a loop, we might need to stop looping after the last character of the string).
PHP Strpos () function
The Strpos () function is used to retrieve the specified character or text within a string.
If a match is found, the first matching character position is returned. If no match is found, FALSE is returned.
The following example retrieves the text "World" in the string "Hello world!":
Instance
<? PHP Echo Strpos ("Hello world!", "World"); >
Running an instance
The output of the above code is: 6.
Tip: The position of the string "World" in the example above is 6. The reason for 6 (instead of 7) is that the position of the first character in the string is 0 instead of 1.
PHP addcslashes () function
Definition and usage
The Addcslashes () function adds a backslash before the specified character.
Grammar
Addcslashes (String,characters)
Parameter description
string is required. Specifies the string to check.
Characters is optional. Specifies the range of characters or characters affected by addcslashes ().
Hints and Notes
Note: Be careful when applying addcslashes () to 0,r,n and T. In PHP, \0,\r,\n and \ t are pre-defined escape sequences.
Example 1
$str = "Hello world!" ; Echo $str ; $str addcslashes ($str, ' A. Z '); $str addcslashes ($str, ' A. Z '); Echo "</br>"; Echo $str;
Hello world!
\h\e\l\l\o \w\o\r\l\d!
PHP addslashes () function
Definition and usage
The Addslashes () function adds a backslash before the specified predefined character.
These predefined characters are:
Single quotation mark (')
Double quotation marks (")
Back slash (\)
Null
Grammar
Addslashes (String)
Parameter description
string is required. Specifies the string to check.
Hints and Notes
Tip: This function can be used to prepare the appropriate string for strings stored in the database and for database query statements.
Note: By default, PHP instruction MAGIC_QUOTES_GPC is on, and automatically runs Addslashes () for all GET, POST, and COOKIE data.
Do not use Addslashes () for strings that have been MAGIC_QUOTES_GPC escaped, because this results in double-layer escaping.
You can use the function GET_MAGIC_QUOTES_GPC () to detect this situation.
<? $str = "I ' m Myall"; Echo addslashes ($str);? >
PHP Bin2Hex () function
PHP String function
Definition and usage
The Bin2Hex () function converts a string of ASCII characters to a hexadecimal value.
Grammar
Bin2Hex (String)
Parameter description
string is required. Specifies the string to convert.
<? $str = "Myall"; Echo Bin2Hex ($str); Echo Pack ("h*",bin2hex($str));? >
Other related functions:
Echo CHR(48);//the 0 Chr () function returns characters from the specified ASCII value. Echo"<br>";$str= "Myall";Echo Chunk_split($str, 1, ".");//The m.y.a.l.l. Chunk_split () function splits a string into a series of smaller parts. Echo"<br>";Echo Convert_uuencode("Myall");//the%;7ea;&p ' Convert_uuencode () function encodes a string using the uuencode algorithm. Echo"<br>";Echo Convert_uudecode(Convert_uuencode("Myall"));//The myall Convert_uudecode () function decodes a uuencode encoded string. Echo"<br>";Print_r(Count_chars("Myall", 1));//Array ([1] + [108] = 2 [109] = 1 [121] = 1)Echo"<br>";Print_r(Count_chars("Myall", 3));//the Almy count_chars () function returns information about the characters used in the string. Echo"<br>";/*The fprintf () function writes a formatted string to the specified output stream (for example, a file or database). The function returns the length of the string being written. The syntax fprintf (stream,format,arg1,arg2,arg++) parameter describes the stream optional. Specifies where to write/output the string. Format required. Conversion format. Arg1 required. Specifies the parameter that is inserted into the first% symbol in the format string. Arg2 is optional. Specifies the parameter that is inserted into the second% symbol in the format string. arg++ is optional. Specifies the parameter that is inserted into the format string third to fourth, and so on, in the% symbol. The description parameter format is the converted form, starting with the percent sign ("%") to the end of the converted character. The following possible format values are: percent%-return percentage symbol%b-binary number%c-character%d according to ASCII value-Signed decimal number%e-sustainable counting method (e.g. 1.5e+3)%u-unsigned decimal number%f-floating point (local setting S aware)%F-floating point (not local settings aware)%o-eight binary%s-string%x-16 binary (lowercase)%x-16 Decimal (uppercase) Arg1, arg2, + + etc parameters will be inserted into the main string percent (%) Symbol. This function is executed step-by. In the first% symbol, insert arg1, insert arg2 at the second% symbol, and so on. */$file=fopen("Test.txt", "W");Echo fprintf($file, "%s hi!",$str);Echo"<br>";Print_r(Explode("A", "Myall"));//Array ([0] = my [1] = ll) the explode () function splits the string into arrays. Echo"<br>";if(crypt_md5==1){ Echo"MD5:".Crypt("Myall");//the Crypt () function returns a string that is encrypted with DES, Blowfish, or MD5. Echo"<br>"; Echo MD5("Myall");}Echo"<br>";printf("%u",CRC32("Myall"));//the CRC32 () function computes the crc32 polynomial of a string. Echo"<br>";Echo htmlentities("I ' m Myall", ent_quotes);//the Htmlentities () function converts a character to an HTML entity. Echo"<br>";Echo Html_entity_decode("i& #039; M Myall", Ent_quotes, "UTF-8");//Echo"<br>";Echo implode("",Array("Hello", "world!"));//the implode () function combines array elements into a single string. The join () function is an alias for the implode () function. Echo"<br>";Parse_str("Id=1&name=myall");//The parse_str () function parses the query string into a variable. Echo $id." <br> ";Echo $name; Echo"<br>";Echo Ord("Myall");//the 109 Ord () function returns the ASCII value of the first character of a string. Echo"<br>";Echo NL2BR("Hello\nmyall");//The nl2br () function inserts an HTML line break (<br>) before each new line (\ n) in the string. Echo"<br>";Echo SHA1("Myall");//the SHA1 () function computes the SHA-1 hash of the string. Echo"<br>";Similar_text("Hello world!", "Hello Myall",$percent);//The Similar_text () function calculates the number of matching characters for two strings. Echo $percent;Echo"<br>";Echo Str_ireplace("World", "Myall", "Hello World");//the Str_ireplace () function replaces some other characters in a string with a string. Echo"<br>";Echo Str_pad("Myall", ".", Str_pad_both);//The Str_pad () function fills a string with the specified length. Echo"<br>";Echo Str_shuffle("Myall");//the Str_shuffle () function randomly scrambles all characters in a string. Echo"<br>";Echo Str_split("Myall", 3);//The str_split () function splits a string into an array. Echo"<br>";Echo strcasecmp("Hello", "Myall");//The strcasecmp () function compares two strings. Echo"<br>";Echo STRCHR("Myall", "a");//the function returns the remainder of the string (from the matching point). returns false if the searched string is not found. Echo"<br>";Echo Strip_tags("//the Strip_tags () function strips HTML, XML, and PHP tags. Echo"<br>";Echo Stristr("Myall", "a");//The Stristr () function finds the position of the first occurrence of a string in another string. Echo"<br>";Echo Stripos("Myall", "a", 0);//The Stripos () function returns the position of the first occurrence of a string in another string. Echo"<br>";Echo substr("Myall", 2, 3);//the substr () function returns a portion of a string. Echo"<br>";Echo Substr_count("Myall", "L", 0,strlen("Myall"));//the Substr_count () function calculates the number of occurrences of a substring in a string. Echo"<br>";Echo Substr_replace("Myall", "QQ", 0,2);//The substr_replace () function replaces part of a string with another string. Echo"<br>";Echo Ucfirst("Myall");//The Ucfirst () function converts the first character in a string to uppercase. Echo"<br>";$str= "Myall";$number= 123;$txt=vsprintf("Hello%s%u",Array($str,$number));//The vsprintf () function writes a formatted string to a variable. Echo $txt;Echo"<br>";$str= "Myall";Echo WordWrap($str, 2, "<br>\n",TRUE);//the WordWrap () function handles string wrapping at a specified length.
PHP String Functions