PHPのstrstr関数の使い方は何ですか?
PHPのstrstr関数は、指定された文字列を検索して、指定された文字列から文字列の末尾までの部分を返すためのものです。
以下は、関数の使用方法です。
strstr(string $haystack, mixed $needle, bool $before_needle = false): string|false
パラメーター説明:
- ヘイスタック:検索対象の文字列。
- 針:検索する文字列。
- needleの前に戻るオプションで、デフォルトはfalseでneedleから文字列の末尾までを返す。
- 戻り値:needle が見つかった場合、needle から文字列の末尾までの部分を返します。見つからない場合は false を返します。
例えば、
$str = 'Hello, world!';
$part = strstr($str, 'world');
echo $part; // 输出:world!
$part2 = strstr($str, 'world', true);
echo $part2; // 输出:Hello,
上記の例では、strstr($str, ‘world’)は文字列内で最初に’world’が現れる場所を見つけ、’world’から文字列の末尾までを返します。一方、strstr($str, ‘world’, true)は’world’より前の部分を返します。