preg_match_all

Discussion in 'PHP' started by ssimon171078, Oct 21, 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, i want to use preg_match_all?
    <?php
    $counter=0;
    $content=file("linux.words");
    foreach ($content as $line)
    {
    //$words = explode(' ', $line); // Split a line by the delimiter "spacer"


    preg_match_all("/[a-z]/i",$line,$result);

    }
    print_r($result)."<br>";
    ?>
     
    ssimon171078, Oct 21, 2014 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    '~a[a-z]+~i'
    
    PHP:
    ... and print_r() after the loop, or add the results to an array and print it after it.

    (Also, replace + with * if you want the word "a" to count as well.)
     
    nico_swd, Oct 21, 2014 IP
  3. ssimon171078

    ssimon171078 Well-Known Member

    Messages:
    277
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    can you write a code ?
     
    ssimon171078, Oct 21, 2014 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Yes, but I won't. :)

    You've got all the pieces, now put them together. At least show me what you've tried.
     
    nico_swd, Oct 21, 2014 IP