0
$string="City"
$consonants=array("B","b","C","c","D","d","F","f","G","g","H","h","J","j","K","k","L","l","M","m","N","n",
"P","p","Q","q","R","r","S","s","T","t","V","v","W","w","X","x","Y","y","Z","z");
foreach($consonants as $chk2)
    {
    if(strpos($string,$chk2)!= false)
        {
        $ans2[]=$chk2;
        }
    }
    if(isset($ans2))
        {
        $comment2="Consonants in this string: ".join(" ",$ans2);
        }
    else
        {
        $comment2="Consonants in this string: None";
        }
echo "".$comment2."<br/>";

so the error is that the displayed consonants does not include the first letter. what could be wrong?

Edited by Dani: Formatting fixed

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by blocblue
0

start quote:

$string="city"
$consonants=array("b","b","c","c","d","d","f","f","g","g","h","h","j","j","k","k","l","l","m","m","n","n",
"p","p","q","q","r","r","s","s","t","t","v","v","w","w","x","x","y","y","z","z");
foreach($consonants as $chk2)
    {
    if(strpos($string,$chk2)!= false)
        {
        $ans2[]=$chk2;
        }
    }
    if(isset($ans2))
        {
        $comment2="consonants in this string: ".join(" ",$ans2);
        }
    else
        {
        $comment2="consonants in this string: None";
        }
echo "".$comment2."<br/>";

so the error is that the displayed consonants does not include the first letter. What could be wrong?

so i found a temporary solution which is having one space before city in the

$string=" city"

Edited by mike_2000_17: Fixed formatting

0

I'm not 100% sure what it is you're trying to do.

If you're trying to find all of the consonants in a given string, what about something like the following. This will include the same letter in $matches multiple times, if it is matched multiple times. To get unique consonants use the commented line at the end.

$vowels = array('a', 'e', 'i', 'o', 'u');
$consonants = array_diff(range('a', 'z'), $vowels);
$word = 'City';
$matches = array();

for($i = 0, $length = strlen($word); $i < $length; $i++) {
    if(in_array(strtolower($word[$i]), $consonants))
        $matches[] = $word[$i];
}

// $matches = array_unique($matches);
print_r($matches);

R.

Edited by blocblue: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.