Strpos()

Discussion in 'Programming' started by ssimon171078, Oct 17, 2014.

  1. #1
    i need to extract all words that begin with "a" in file linux.words but i started to write code in php but i have problem i receive all words that contain "a" ,how to fix it ?
    <?php
    $str=a;
    $counter=0;
    $content=file("linux.words");
    foreach ($content as $line)
    {
    $d=strpos($line,"a");
    if($d==1){
    echo $line."<br>";

    $counter++; }
    }
    echo "counter=".$counter."<br>";
    ?>
     
    ssimon171078, Oct 17, 2014 IP
  2. jslirola

    jslirola Active Member

    Messages:
    39
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    58
    #2
    <?php
    
    $counter=0;
    $content=file("linux.words");
    
    foreach ($content as $line)
    {
    
    $words = explode(' ', $line); // Split a line by the delimiter "spacer"
    for ($i=0; $i < count($words) ; $i++) {
        if($words[$i][0]=="a") { // First letter of each word
            echo $words[$i]."<br />";
            $counter++;
        }
    }
    
    }
    echo "counter=".$counter."<br>";
    
    ?>
    PHP:
     
    jslirola, Oct 17, 2014 IP