This is a discussion on preg_match_all within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi I'm trying to extract from a line of HTML individual tags, using the "name" attribute to ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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" <mike@nospam.mak.co.nz> wrote in message news:429674e2@news.maxnet.co.nz... > 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 > |
|
|||
|
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" <vietquest@hotmail.com> wrote in message news:mZmdndeH6oMdGgvfRVn-gw@mycybernet.net... > 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" <mike@nospam.mak.co.nz> wrote in message > news:429674e2@news.maxnet.co.nz... >> 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 >> > > |
|
|||
|
Hi Du
Du wrote: > 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".*?>!' ; You're a legend in your own time :-) Thanks for that, it works perfectly ! Mike > > > > "Du" <vietquest@hotmail.com> wrote in message > news:mZmdndeH6oMdGgvfRVn-gw@mycybernet.net... >> 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" <mike@nospam.mak.co.nz> wrote in message >> news:429674e2@news.maxnet.co.nz... >>> 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 >>> >> >> |
|
|||
|
> You're a legend in your own time :-)
hahaha .... :D i'm glad that it works !!! "Mike M" <mike@nospam.mak.co.nz> wrote in message news:4296d6bf@news.maxnet.co.nz... > Hi Du > > Du wrote: > >> 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".*?>!' ; > > You're a legend in your own time :-) > > Thanks for that, it works perfectly ! > > Mike > >> >> >> >> "Du" <vietquest@hotmail.com> wrote in message >> news:mZmdndeH6oMdGgvfRVn-gw@mycybernet.net... >>> 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" <mike@nospam.mak.co.nz> wrote in message >>> news:429674e2@news.maxnet.co.nz... >>>> 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 >>>> >>> >>> > ----== 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 =---- |