Home > Article > Backend Development > PHP string function learning strstr()_PHP tutorial
This article mainly introduces strstr() for learning PHP string function. This article explains its definition, usage and parameters. Description, tips and comments as well as multiple usage examples, friends in need can refer to it
The code is as follows:
Parameter Description
string required. Specifies the string to be searched for.
search required. Specifies the string to search for. If the argument is a number, searches for characters matching the numeric ASCII value
Tips and Notes
Note: This function is binary safe.
Note: This function is case sensitive. For case-insensitive searches, use stristr().
If you just want to determine whether the needle exists in the haystack, use the faster and less memory-intensive strpos() function.
*/
$str = ".The specified string to be searched";
$s = "of";
echo strstr($str,$s) . "
";
echo mb_strstr($str,$s, 'utf-8') . "
";
echo mb_strstr($str,$s, true, 'utf-8') . "
";
echo mb_strstr($str,$s, false, 'utf-8') . "
/*
Definition and Usage
The strrchr() function finds the last occurrence of a string within another string and returns all characters from that position to the end of the string.
If successful, return false otherwise.
Grammar
strrchr(string,char)
Parameter Description
string required. Specifies the string to be searched for.
char required. Specifies the characters to search for. If the argument is a number, searches for characters matching the numeric ASCII value.
*/
echo strrchr($str,$s) . "
";
echo mb_strrchr($str,$s, 'utf-8') . "
";
echo mb_strrchr($str,$s, true, 'utf-8') . "
";
echo mb_strrchr($str,$s, false, 'utf-8') . "
$str = "Hello world!Hello world!";
$s = "world";
echo strrchr($str,$s) . "
";
echo mb_strrchr($str,$s, 'utf-8') . "
";
echo mb_strrchr($str,$s, true, 'utf-8') . "
";
echo mb_strrchr($str,$s, false, 'utf-8') . "
/*
Definition and Usage
The strtr() function converts specific characters in a string.
Grammar
strtr(string,from,to)
or
strtr(string,array)
Parameter Description
string1 required. Specifies the string to be converted.
from Required (unless using an array). Specifies the character to be changed.
to Required (unless using an array). Specifies the character to change to.
array Required (unless from and to are used). An array where the keys are the original characters and the values are the target characters.
Description
If from and to are of different lengths, format to the shortest length.
*/
echo strtr("Hilla Warld","ia","eo"). "
";
$arr = array("Hello" => "Hi", "world" => "earth");
echo strtr("Hello world",$arr). "
echo strtr("If omitted, use internal character encoding", "internal", "external"). "
";
$arr = array("if" => "if", "then" => "then");
echo strtr("If omitted, internal character encoding is used",$arr). "