PHP – I need the position of the first vowel in a name found

Q: What I want to do is find the position (a) of the first vowel in a user name and then the output and use it in a password. What I have is this, but it sucks . Frigg Im lost . heading the wrong way and I have a little help:

function firstvowel () (
include (admin_connect.php); $ 977.503 = query (“SELECT name FROM users WHERE username =”.$_ SESSION [username] .”"); $ 977.503 $ result = db_object u003e-query ($ query);

if (DB:: isError ($ result)) (977 503 die ($ result-u003e GetMessage ()); 977 503 977 503) $ row = $ result-u003e fetchRow (DB_FETCHMODE_ASSOC) $ 977.503 firstname = $ row [name], $ 977.503 for name = trim ($ name); $ 977.503 countvowel strpos = ($ name, (eregi (“^ [aeiou] $ , $ name))) 1;
return $ countvowel; 977 503)

Thanks in advance,

SHUX


Re:^ there is the more "elegant" way :D

Re:You can also use preg_match combined with strpos to avoid the looping through the string.

Re:Awesome! Very cool little function… and I learned something… :)

SHUX


Re:Originally posted by: guy
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…


Re: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…

Re:Originally posted by: guy
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.


Re: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


Re: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


Re:Can you explain what you want from the string again, you want the string from index 0 to the first occurence of a vowel?

Re: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


Re: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


Re:Originally posted by: guy
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


Re:add a counter and edit the condition statement to compensate for if the first letter is a vowel

Re:Originally posted by: guy
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


Re: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…


Re:the +1 and -1 is redundant

Re:This did it perfectly! Thanks!

SHUX


Re:Still not sure by what you mean with +1…

Re:Originally posted by: guy
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


Re: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.

Re:I tried this, it still only shows '1'

SHUX


Related posts

Leave a comment

0 Comments.

Leave a Reply


click to changeSecurity Code

[ Ctrl + Enter ]