Member Avatar for rayrenz
$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?

Recommended Answers

All 3 Replies

Member Avatar for rayrenz

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"

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.

hai bro . i need your help bro

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.