Curl and regex

Packers

Registered Member
Joined
Jan 31, 2011
Messages
77
Reaction score
7
Hey guys,

I can't see why the following isn't working on yahoo


Code:
  preg_match_all('/<\s*form[^<>].*?name=[\'"]?(.*?)?[\s\'"].*>.*?<\/form>/',$data,$match);

If i remove the .*?<\/form> it finds the right thing. any ideas where i'm screwing up? Think it worked for google...
 
im not great with regex, but i can try help
what you trying to do here?
 
im not great with regex, but i can try help
what you trying to do here?

I'm just trying to get the name of all the forms on the page. Its a little task for me to learn some regex. Really frustrating though!

So, here's where I am

Code:
preg_match_all('/<\s*form[^<>].*?name=[\'"]?(.*?)?[\s\'"].*>/i',$data,$match);

This returns

Array
(
[0] => Array
(
[0] => <form role="search" name="sf1" method="get" id="p_13838465-searchform" class="search-form" action="linkwhichicannotpost">
)

[1] => Array
(
[0] => sf1
)

)

for yahoo. Which is good.

This on the other hand,

Code:
preg_match_all('/<\s*form[^<>].*?name=[\'"]?(.*?)?[\s\'"].*>(.*?)<\/form>/i',$data,$match);

returns an empty array.

Thinkin I should play around with /s tag
 
Code:
preg_match_all('/<\s*form[^<>].*?name=[\'"]?(.*?)?[\s\'"].*>(.*?)<\/form>/si',$data,$match);

Still an empty array... Must be something wrong in (.*?) now I think?
 
im asking my clever buddy on skype quick, gimme 2 minutes hopefully he can help, he is a regex nerd :)

update: he is having a look at it
 
Last edited:
Thanks :) Regex is so confusinggggg! :p

EDIT: I'l have to try whatever he says when I get back from uni :( Will be a few hours. Will let you know how it goes. Thanks again!
 
Last edited:
ok he says you should just try get it with strpos from the first result:

Code:
preg_match_all('/<\s*form[^<>].*?name=[\'"]?(.*?)?[\s\'"].*>/i',$data,$match);

because you have the entire form field in the array there. so find the position of the name=" part of the return data, and get all the characters after that until the next "

i can do it for you quick if you need help

PM me when you back if you need help
 
Last edited:
ok he says you should just try get it with strpos from the first result:

Code:
preg_match_all('/<\s*form[^<>].*?name=[\'"]?(.*?)?[\s\'"].*>/i',$data,$match);

because you have the entire form field in the array there. so find the position of the name=" part of the return data, and get all the characters after that until the next "

i can do it for you quick if you need help

PM me when you back if you need help

The thing is, I get the right result when I just have that code above. It works. I don't follow why it doesnt work when I try to find whats in between the <form ...>more stuff i want to store</form>.
In the end I'd like to be able to grab every tag on a page, and every attribute of that tag and its values. I'm sure regex alone should be able to handle this!

It appears that there is a mistake with >(.*?)<\/form> and I thought this might be down to the new lines and stuff in the source code. Mmmm
 
I would recommend using simple_html_dom class when dealing with DOM. it is less error prone and much more simple to use, especially for what you want.
 
I'm just trying to get the name of all the forms on the page. Its a little task for me to learn some regex. Really frustrating though!

So, here's where I am

Code:
preg_match_all('/<\s*form[^<>].*?name=[\'"]?(.*?)?[\s\'"].*>/i',$data,$match);
This returns

Array
(
[0] => Array
(
[0] => <form role="search" name="sf1" method="get" id="p_13838465-searchform" class="search-form" action="linkwhichicannotpost">
)

[1] => Array
(
[0] => sf1
)

)

for yahoo. Which is good.

This on the other hand,

Code:
preg_match_all('/<\s*form[^<>].*?name=[\'"]?(.*?)?[\s\'"].*>(.*?)<\/form>/i',$data,$match);
returns an empty array.

Thinkin I should play around with /s tag

Ok answer for your first question

PHP:
preg_match_all('|<form[^<]*name=[\'"](.*?)[\'"][^<]*>|', $from, $form);
Which returns you all form names if there is an name for form.
for your yahoo test getting "sf1" as only form name is correct cause there is some more forms but without names.

For your second one i can see that youll wish to get data between form tags with specific form name

PHP:
preg_match_all('|<form[^<]*name=[\'"](.*?)[\'"][^<]*>|', $from, $form);

foreach($form[1] as $value)
{
    preg_match('|<form[^<]*'.$value.'[^<]*>(.*?)</form>|', $from, $forms);

     print_r($forms[1]);
}
Which will display you all data between form tags form all form names.
 
Last edited:
Hm seems that you have to clean your html from unvanted characters which will break regex ok here's an edited version

PHP:
    // Whatever your using to grab content file_get_contents or curl output holds your html
    $output = "YOUR HTML HERE";
    $replacement = array('/\s\s+/','/\v/');
    $from = preg_replace($replacement,' ', $output);
    
    preg_match_all('|<form[^<]*name=[\'"](.*?)[\'"][^<]*>|', $from, $form);

    foreach($form[1] as $key => $value)
    {
        preg_match('|<form[^<]*name=[\'"]'.$value.'[\'"][^<]*>(.*?)</form>|', $from, $forms);

        print_r($forms);
    }
Now that's an badass solution ;)
 
Back
Top