PHP 文字列の特定の文字の位置を検索するサンプル
環境
PHP 8.1.2
Ubuntu 22.04.1 LTS
構文
$変数名= strpos($対象の文字列, 検索する文字);
文字列(string)の特定の文字の位置を検索するには、strpos()を使います。
第1引数に対象の文字列、第2引数に位置を検索する文字を指定します。
使用例
<?php
$text = "ARKGAME";
$result = strpos($text, "A");
$result2 = strpos($text, "E");
echo $result . "\n";
echo $result2;
?>
<?php
$text = "ARKGAME";
$result = strpos($text, "A");
$result2 = strpos($text, "E");
echo $result . "\n";
echo $result2;
?>
<?php $text = "ARKGAME"; $result = strpos($text, "A"); $result2 = strpos($text, "E"); echo $result . "\n"; echo $result2; ?>
実行結果
0
6