- 使用while循环来查找字符串中的特定字符
$string = "Hello World"; $char = 'o'; $pos = 0; while (($pos = strpos($string, $char, $pos)) !== false) { echo "Found $char at position $pos
"; $pos++; }
- 使用for循环来查找字符串中的特定字符
$string = "Hello World"; $char = 'o'; for ($i = 0; $i < strlen($string); $i++) { if ($string[$i] == $char) { echo "Found $char at position $i
"; } }
- 使用foreach循环来查找字符串中的特定字符
$string = "Hello World"; $char = 'o'; $chars = str_split($string); foreach ($chars as $key => $value) { if ($value =https://www.yisu.com/ask/= $char) {"Found $char at position $key
"; } }