preg_match_all

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 ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-27-2005
Mike M
 
Posts: n/a
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

Reply With Quote
  #2 (permalink)  
Old 05-27-2005
Du
 
Posts: n/a
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" <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
>



Reply With Quote
  #3 (permalink)  
Old 05-27-2005
Du
 
Posts: n/a
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" <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
>>

>
>



Reply With Quote
  #4 (permalink)  
Old 05-27-2005
Mike M
 
Posts: n/a
Default Re: preg_match_all

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
>>>

>>
>>


Reply With Quote
  #5 (permalink)  
Old 05-27-2005
Du
 
Posts: n/a
Default Re: preg_match_all

> 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 =----
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:13 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0