我需要找到在php字符串中的第一个元音的位置


I need to find the position of the first vowel in a string in php

我需要找到一个单词字符串中第一个元音的位置。为例。

  • garbage将返回1
  • plastic将返回2
  • apple返回0

我是初学者,所以我有一个困难的时间。我想我需要一个带有数组或正则表达式的循环。

使用strcspn函数。它将返回开头不匹配您指定的任何字符的字符串长度。

$pos = strcspn(strtolower($str), "aeiou"); // and sometimes y

<?php $mystring="find first vowel in a string";
$vowel=array();
$vowel['0'] = stripos($mystring,'a');
$vowel['1'] = stripos($mystring,'e');
$vowel['2'] = stripos($mystring,'i');
$vowel['3'] = stripos($mystring,'o');
$vowel['4'] = stripos($mystring,'u');
$min=99999;
for($i=0; $i<5;$i++){
if ($vowel[$i] !== false) {
if($min>$vowel[$i]){
$min=$vowel[$i];
}//end if
}//end if
}end for
if($min==99999){
echo "No vowel found.";
}else{
echo "vowel found at position :".$min;
}
?>

strpos -查找子字符串在字符串

中第一次出现的位置

如= =

  $email  = 'name@example.com';
  $domain = strpos($email, '@');
  echo $domain; // prints 4