Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Feb 2007
    Location
    London
    Posts
    225
    Thanks
    16
    Thanked 2 Times in 2 Posts

    strpos with a $needle as all the values of an array

    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?

    PHP Code:
    //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

  • #2
    New Coder
    Join Date
    Mar 2010
    Location
    Virginia, USA
    Posts
    25
    Thanks
    0
    Thanked 5 Times in 5 Posts
    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 function which does take an array to make the word you are processing simpler to process.

    PHP Code:
    //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

  • #3
    New Coder
    Join Date
    Mar 2010
    Location
    Virginia, USA
    Posts
    25
    Thanks
    0
    Thanked 5 Times in 5 Posts
    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

  • Users who have thanked hoothelp for this post:

    cfructose (03-15-2010)

  • #4
    Regular Coder
    Join Date
    Feb 2007
    Location
    London
    Posts
    225
    Thanks
    16
    Thanked 2 Times in 2 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •