In this post we are going to see how to write a PHP script to strip vowels from string. This post could be useful for school / college students or beginners PHP learners.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<html> <head> <title>PHP Script to Strip Vowels from String - Tutorialsmade.com</title> </head> <body> <h2>PHP Script to Strip Vowels from String - <a href="http://www.tutorialsmade.com/">Tutorialsmade.com</a></h2> <form action="" method="post"> <input type="text" name="string" /> <input type="submit" /> </form> </body> </html> <?php if($_POST) { //get the input value $string = $_POST['string']; //all vowels in an array $vowels = array('a','e','i','o','u','A','E','I','O','U'); //find length of the string $len = strlen($string); $num = 0; $only_vowels = ''; //loop through each letter for($i=0; $i<$len; $i++){ //if vowel letter found concat in $only_vowels; if(in_array($string[$i], $vowels)) { $only_vowels .= $string[$i]; } } //if vowels found if(strlen($only_vowels) > 0){ //print all the vowels print "<b>Stripped vowels:</b><br>"; print $only_vowels."<br><br>"; //also print the counts of each vowel print "<b>Each vowel count: <br></b>"; foreach(count_chars($only_vowels, 1) as $k=>$v){ echo chr($k).' '. $v .' time(s) <br>'; } }else{ echo "No vowels found."; } } ?> |
Just read the in-line comments to understand the script.
Example output of the above script:
1 2 3 4 5 6 7 8 9 10 11 |
Input String: This is just an example Stripped vowels: iiuaeae Each vowel count: a 2 time(s) e 2 time(s) i 2 time(s) u 1 time(s) |
You can see the demo of the above script from here:
Enjoy learning!