Home  >  Article  >  Backend Development  >  PHP string function learning strstr()_PHP tutorial

PHP string function learning strstr()_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:00:081123browse

strstr() for learning PHP string function

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:


/*
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, 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). "


";
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/975117.htmlTechArticleStrstr() for learning PHP string functions. This article mainly introduces strstr() for learning PHP string functions. , this article explains its definition and usage, parameter description, tips and comments, and multiple uses...
Statement:
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