Home > Article > Backend Development > PHP string function learning strstr(), string strstr_PHP tutorial
Copy code The code is as follows:
/*
Definition and Usage
The strstr() function searches for the first occurrence of one string within another string.
This function returns the rest of the string (from the matching point). Returns false if the searched string is not found.
Grammar
string strstr (string, search)
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, then 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). "