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