Professional Web Applications Themes

preg_match_all - PHP Development

Hi I'm trying to extract from a line of HTML individual tags, using the "name" attribute to determine which tag to extract. What I'm getting seems to be a greedy match which isn't what I'm looking for. // source html line with rubbish tag for testing purposes $LS_HTML_LINE='<input type="text" value="456=tre" width=5 name="item_2" blah=wer checked id="abc" />This is not to be included</a><input type="checkbox" value="123=tre" name="item_1" checked />' ; // regular expression to use $LS_LOOK_FOR_MASK='!<[^/].*?\sname="item_1".*?>!' ; // run the regular expression preg_match($LS_LOOK_FOR, $LS_HTML_LINE, $LA_ALL_TAGS) ; // lets see what we've got print_r($LA_ALL_TAGS) ; Array ( [0] => <input type="text" value="456=tre" width=5 name="item_2" ...

  1. #1

    Default preg_match_all

    Hi

    I'm trying to extract from a line of HTML individual tags, using the "name"
    attribute to determine which tag to extract. What I'm getting seems to be a
    greedy match which isn't what I'm looking for.

    // source html line with rubbish tag for testing purposes
    $LS_HTML_LINE='<input type="text" value="456=tre" width=5 name="item_2"
    blah=wer checked id="abc" />This is not to be included</a><input
    type="checkbox" value="123=tre" name="item_1" checked />' ;
    // regular expression to use
    $LS_LOOK_FOR_MASK='!<[^/].*?\sname="item_1".*?>!' ;
    // run the regular expression
    preg_match($LS_LOOK_FOR, $LS_HTML_LINE, $LA_ALL_TAGS) ;
    // lets see what we've got
    print_r($LA_ALL_TAGS) ;

    Array
    (
    [0] => <input type="text" value="456=tre" width=5 name="item_2" blah=wer
    checked id="abc" />This is not to be included</a><input type="checkbox"
    value="123=tre" name="item_1" checked />
    )

    This is not what I'm after, the desired result is :
    <input type="checkbox" value="123=tre" name="item_1" checked />

    but when I do the same for item_2 I get the desired result

    $LS_LOOK_FOR_MASK='!<[^/].*?\sname="item_2".*?>!' ;
    preg_match($LS_LOOK_FOR, $LS_HTML_LINE, $LA_ALL_TAGS) ;
    print_r($LA_ALL_TAGS) ;
    Array
    (
    [0] => <input type="text" value="456=tre" width=5 name="item_2" blah=wer
    checked id="abc" />
    )

    This tells me that I'm missing something in the beginning of the regular
    expression, I've spent several hours hacking to no effect and Googling
    without seeing anything that seems to be what I'm after.

    I think that it's time to throw this one out to those more proficient with
    regular expressions. Any help would be greatly appreciated.

    Cheers
    Mike

    Mike Guest

  2. #2

    Default Re: preg_match_all

    try this

    // $LS_LOOK_FOR_MASK='!<[^/].*?\sname="item_1".*?>!' ;

    instead of .*? at the end ..... use [^>]*?

    $LS_LOOK_FOR_MASK='!<[^/].*?\sname="item_1"[^>]*?>!' ;


    i haven't test it out yet ... try it out, if it doesn't work let me know



    "Mike M" <mak.co.nz> wrote in message
    news:maxnet.co.nz... 


    Du Guest

  3. #3

    Default Re: preg_match_all

    sorry, i should have read your question more carefully
    i thot you wanted the first <input>

    try this then

    $LS_LOOK_FOR_MASK='!<[^<]*?\sname="item_1".*?>!' ;



    "Du" <com> wrote in message
    news:net... 
    >
    >[/ref]


    Du Guest

  4. #4

    Default Re: preg_match_all

    Hi Du

    Du wrote:
     

    You're a legend in your own time :-)

    Thanks for that, it works perfectly !

    Mike
     
    >>
    >>[/ref][/ref]

    Mike Guest

  5. #5

    Default Re: preg_match_all

    > You're a legend in your own time :-)

    hahaha .... :D

    i'm glad that it works !!!


    "Mike M" <mak.co.nz> wrote in message
    news:maxnet.co.nz... 
    >
    > You're a legend in your own time :-)
    >
    > Thanks for that, it works perfectly !
    >
    > Mike
    > [/ref]
    >[/ref]



    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Du Guest

Similar Threads

  1. #27011 [Opn]: Seg Fault During preg_match_all
    By ehicks in forum PHP Development
    Replies: 3
    Last Post: January 27th, 10:19 PM
  2. #27011 [Opn->Fbk]: Seg Fault During preg_match_all
    By sniper@php.net in forum PHP Development
    Replies: 4
    Last Post: January 27th, 09:17 AM
  3. preg_match VS preg_match_all
    By Han in forum PHP Development
    Replies: 2
    Last Post: October 4th, 09:21 PM
  4. [PHP] preg_match_all
    By Curt Zirzow in forum PHP Development
    Replies: 0
    Last Post: September 23rd, 06:16 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139