...

View Full Version : strpos with a $needle as all the values of an array



cfructose
03-14-2010, 12:20 AM
I'm trying to find the position of the first vowel in a string, where the vowels are written in Kirshenbaum phonetic script.

Is there a php inbuilt function similar to strpos, but where the position in the string we're searching is returned based on any value from an array, rather than just a '$needle' string?

I ask because the following code, while succinct enough, seems a little on the convoluted side for something so simple. Could it be rewritten without the for loop?
Any suggestions for improvement?


//Array of symbols used to represent vowels in a system not unliek X-SAMPA
$Kirshenbaum['vowels'] = array(
'A', '&', 'a', 'V', '@', 'e', '3', 'I', 'i', '0', 'O', 'o', 'u', 'U'
);

for ($i=0; $i<count($Kirshenbaum['vowels']); $i++) {
$pos = strpos($word, $Kirshenbaum['vowels'][$i]);
if ($pos !== FALSE) {
break;
}
}

Thanks

hoothelp
03-14-2010, 05:40 AM
Hello.

Here's an alternative - though you may want to benchmark yours and mine to see which comes out on top. As I'm not aware of any function which does position location with an array - I use the string replacement (http://www.php.net/manual/en/function.str-replace.php) function which does take an array to make the word you are processing simpler to process.



//Array of symbols used to represent vowels in a system not unlike X-SAMPA
$kirshenbaum['vowels'] = array(
'A', '&', 'a', 'V', '@', 'e', '3', 'I', 'i', '0', 'O', 'o', 'u', 'U'
);

$simpleWord = str_replace( $kirshenbaum['vowels'], '@', $word );
$pos = strpos( $simpleWord, '@' );


Take the word you are processing, replace all it's vowels with a filler character (best to choose a character which is already considered a vowel so you don't get a false positive when doing the strpos function) and save it to a temporary variable which you call the strpos function on. You can then use the $pos variable to identify where the first vowel is with the original, unmodified $word variable.

This saves you from the loop. Then it's a question of whether the loop or the str_replace function is faster in execution. And that will likely depend on how long the word is, how many vowels it has, and where the first vowel occurs.

- C. Aaron Smith

hoothelp
03-14-2010, 05:49 AM
Also - wouldn't you likely get a false result by breaking from the loop as soon as a vowel is found.

If $word = 'BolA' (excuse the dummy word, I'm not familiar with Kirshenbaum phonetic script), then your loop with do strpos with the 'A' vowel first. Its position gets assigned to $pos and the loop is broken. While 'o' is actually the first vowel in the word.

You would need to ensure that strpos gets performed on the word for every vowel in the array, and then compare the positions to see which is first.

Maybe that's already being done and the code example was simplified -thought I should point it out if that's not the case :)

- C. Aaron Smith

cfructose
03-15-2010, 03:49 AM
Brilliant! Thank you!

Your suggestion of replacing all the characters first is inspired. I would never have thought of that.

(Yes, I was simplifying the code as you guessed in your second post, so no problem there.)

Again, thank you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum