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 6 of 6
  1. #1
    Regular Coder grumpy's Avatar
    Join Date
    Oct 2006
    Location
    Visible light spectrum
    Posts
    121
    Thanks
    5
    Thanked 6 Times in 6 Posts

    How to replace non-vowels in a string?

    I am trying to extract vowels from a string. One thing is to replace anything that is not a,e,i,o,u with "".

    PHP Code:
    $fname_out "test";

    $vowels = array ('a''e''i''o''u'); 
    One option (does not work) I considered:
    PHP Code:
    $vow_fname preg_replace(array('/^[^$vowels]+$/'), ''$fname_out); 
    The other (does not work):
    PHP Code:
    $vow_fname preg_replace('/^[^aeiou]+$/'''$fname_out); 
    And another (does not work):
    PHP Code:
    $vow_fname str_replace(!($vowels), ""$fname_out); 
    Could you please suggest the right solution. I have been scouring php.net and other sites, but cannot seem to get the solution

    Thank you!

    Bo
    Last edited by grumpy; 08-08-2008 at 10:44 PM.

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Re-read it. Will do another example in a min
    Last edited by Nightfire; 08-08-2008 at 10:28 PM.

  • #3
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,285
    Thanks
    4
    Thanked 201 Times in 198 Posts
    Wouldn't this work for what you're trying to accomplish?

    PHP Code:
    $vow_fname str_replace($vowels," ",$fname_out); 
    All of the vowels should be spaces in $vow_fname.
    Last edited by djm0219; 08-08-2008 at 10:27 PM. Reason: Typo
    Dave .... HostMonster for all of your hosting needs

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    close with your second preg_replace one, but you don't want to restrict it as much as you have. Your regular expression is saying 'replace one or more successive consonants betweent the start and end of the string', which isn't what you want.
    PHP Code:
    $result preg_replace('/[^aeiou]/i','',$source); 
    should do what you want.
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • Users who have thanked GJay for this post:

    grumpy (08-08-2008)

  • #5
    Regular Coder grumpy's Avatar
    Join Date
    Oct 2006
    Location
    Visible light spectrum
    Posts
    121
    Thanks
    5
    Thanked 6 Times in 6 Posts
    GJay, it works a treat!

    Thanks a billion!

    Cheers,

    Bo

  • #6
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    I found this, doesn't delete punctuation.

    PHP Code:
    <?php 
    $string 
    ="this is a test which should delete all consonants, yes?";
    $string preg_replace('/(?:[bcdfghjklmnpqrstvwxyz]|(?<=[aeiou]))/',''$string);
    echo 
    $string;
    ?>
    Probably OTT and could probably be done easily in gjay's code somehow.


  •  

    Posting Permissions

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