PHP - I need to find the position of the first vowel in a name

Shuxclams

Diamond Member
Oct 10, 1999
9,286
15
81
What I want to do is find the position (+1) of the first vowel in a users name and then take that output and use it in a password. What I have is this, but it friggen sucks... I am lost now.. heading down the wrong road and I need a bit help:

function firstvowel(){
include('admin_connect.php');
$query=("SELECT first_name FROM users WHERE username='".$_SESSION['username']."'");
$result = $db_object->query($query);

if (DB::isError($result)) {
die($result->getMessage());
}
$row = $result->fetchRow(DB_FETCHMODE_ASSOC);
$firstname=$row['first_name'];
$firstname=trim($firstname);
$countvowel=strpos($firstname, (eregi('^[aeiou]$', $firstname)))+1;
return $countvowel;
}



Thanks in advance,

SHUX
 

Drakkon

Diamond Member
Aug 14, 2001
8,401
1
0
eregi outputs a truw/false thats why your only getting a 1 no matter what...
prettymuch the only way to do it would be to loop throuwh an array of vowels and strpos each one and if ok then break i.e.
 

Shuxclams

Diamond Member
Oct 10, 1999
9,286
15
81
Originally posted by: Drakkon
eregi outputs a truw/false thats why your only getting a 1 no matter what...
prettymuch the only way to do it would be to loop throuwh an array of vowels and strpos each one and if ok then break i.e.



So lets take the name 'John', I want to show that first vowel in position '2' as the second letter of the name, this will only output the first iteration?


SHUX
 

Drakkon

Diamond Member
Aug 14, 2001
8,401
1
0
actually I'm sorry to bum ya shux but it wont work "perfectly"...what is the name is Isaac? check out what it outputs as being the position of the first "vowel"...

sorry tryign to come up with a good soln to that...
 

Shuxclams

Diamond Member
Oct 10, 1999
9,286
15
81
Originally posted by: Drakkon
actually I'm sorry to bum ya shux but it wont work "perfectly"...what is the name is Isaac? check out what it outputs as being the position of the first "vowel"...

sorry tryign to come up with a good soln to that...



yeah I just noticed that.............. It worked good once?





SHUX
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
add a counter and edit the condition statement to compensate for if the first letter is a vowel
 

Shuxclams

Diamond Member
Oct 10, 1999
9,286
15
81
Originally posted by: tfinch2
add a counter and edit the condition statement to compensate for if the first letter is a vowel

:confused:




I tried modifying the condition, havent quite figured it out yet......




SHUX
 

Shuxclams

Diamond Member
Oct 10, 1999
9,286
15
81
I dont seem to be able to figure out a counter, I can use count(), array_count_values() with some type of next()?

Help :)










SHUX
 

Shuxclams

Diamond Member
Oct 10, 1999
9,286
15
81
Here is the latest and greatest.... It returns a '0' everytime... I think the problem is in the 'if($countvowel > 0)' condition... or near it.. LoL


Any ideas?








SHUX
 

wkinney

Senior member
Dec 10, 2004
268
0
0
Can you explain what you want from the string again, you want the string from index 0 to the first occurence of a vowel?
 

Shuxclams

Diamond Member
Oct 10, 1999
9,286
15
81
I need to grab the numeric position of the first vowel in the first name of the user. Since PHP counts the first char as '0' I need to add 1 to the position number. i.e, the first name John the first vowel is 'o', PHP function strpos() will report that as position 1 when it is actually the second letter in the name. Also when the first letter is a vowel strpos() reports '0' so I need to make sure that it returns a '1'. Now the odd ball thing is the error control..... If it fails it returns.... you guessed it = '0'








SHUX
 

Shuxclams

Diamond Member
Oct 10, 1999
9,286
15
81
So this returns the following;

Sean = 2
Dave = 1
Rich = 0
Geoff = 0
Yolanda = 3

What I see in this is that the array is not looping, its searches for an 'a' and when it doesnt find an 'a' it returns the error '0'. Another issue is adding the +1 to the strpos() for the desired output.







SHUX
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Originally posted by: Shuxclams
So this returns the following;

Sean = 2
Dave = 1
Rich = 0
Geoff = 0
Yolanda = 3

What I see in this is that the array is not looping, its searches for an 'a' and when it doesnt find an 'a' it returns the error '0'. Another issue is adding the +1 to the strpos() for the desired output.






SHUX

The function finds the position of the first 'a' and returns it.
 

Drakkon

Diamond Member
Aug 14, 2001
8,401
1
0
strpos is a crappy function...is has duel return types which is horrible because php defines 0 == false (thus it returns 0 if the first character is equal to a "A" or the word has no a's in it)
attached is a working version without calling db so have to modify...i dont know about elegant solution but it seems to work...
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Originally posted by: Drakkon
strpos is a crappy function...is has duel return types which is horrible because php defines 0 == false (thus it returns 0 if the first character is equal to a "A" or the word has no a's in it)
attached is a working version without calling db so have to modify...i dont know about elegant solution but it seems to work...

No wonder this wouldn't work...
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
You can also use preg_match combined with strpos to avoid the looping through the string.