0
1< form name=form_prod action="./conso_cond.php" method="post"><br />
2< form name=open_belg_certif target="_blank" method=post action="./show_certif.php"><br />
3< form method = "get" action=""><br />
4< form><br />
5< form method='get'><br />
6< form name="frm" method='get' action=""><br />
7< form method=get action=""><br />
8< form action=""><br />
9< form action="TT<? echo $p; ?>" method="get"><br />
10< form action="forma<? echo $p; ?>" action="top.php" target="top" method="get"><br />

I want the regular expression to get the all the FORMs submitting as GET not POST. That means method="get" mentioned or not mentioned or nothing mentioned in the FORM tag.

If someone want to try my code:
$arr[] = '1< form name=form_prod action="./conso_cond.php" method=post">';
$arr[] = '2< form name=open_belg_certif target="_blank" method=post action="./show_certif.php">';
$arr[] = '3< form method = "get" action="">';
$arr[] = '4< form>';
$arr[] = '5< form method="get">';
$arr[] = '6< form name="frm" method="get" action="">';
$arr[] = '7< form method=get action="">';
$arr[] = '8< form action="">';
$arr[] = '9< form action="TT" method="get">';
$arr[] = '10< form action="forma" action="top.php" target="top" method="get">';

echo '< textarea cols=100 rows=30>';

foreach($arr as $myLine) {
if (preg_match("/[\s]*<[\s]form((.)method=['\"]?get['\"]?).>/", $myLine)) {
echo "\n". trim($myLine);

}
}
echo '< /textarea>';

This code returns the unwanted cases 1 and 2 (both are POSTs)!.
Thanks in advance!

San
  • 666
  • 7
  • 27
  • possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – Madara's Ghost Jul 26 '13 at 14:22
  • I want to search in the .php files using regular expression.What I want is I have a project with very old code. I want to migrate it to PHP5.4. In the current source code we cannot identify the variables are normal/GET/POST variables. So I want to collect all the variables which is possibly submitted as GET. Later I will check for POST variables. – San Jul 26 '13 at 14:32
  • @Madara-Uchiha, This question is for using **regular expression** how can we get all the forms submitted as GET. – San Jul 26 '13 at 14:44
  • The correct answer is that you don't use regex to parse HTML. Hence the linked duplicate. – Madara's Ghost Jul 26 '13 at 14:45
  • The thing is I have created all the variables from query string in the .php files to corresponding files. This task is blocking me. ie. "filename.php?var1=value1&var2=val2" these kind of variables I created in corresponding files through the script. Because my project have 40K+ .php files!. So as much as I can, I want to make script to modify the files. If the question is clear please help me to follow what I wrote already. – San Jul 26 '13 at 14:47
  • $line = '< form action="forma echo $p; ?>" action="top.php" target="top" method="get">
    '; if (preg_match("/[\s]*
    /", $line)) { echo "A form found here"; }
    – San Jul 26 '13 at 14:52

1 Answers1

0

Try this regular expression:

"/\<form(.+?)method=['|\"]get['|\"]/i"
CoursesWeb
  • 4,179
  • 3
  • 21
  • 27
  • Sorry @CoursesWeb, I don't know how to format my reply. When I tried your pattern it returned the cases 5,6,9 and 10. But for me except cases 1 and 2(both are POSTs) all are valid FORM gets. – San Jul 27 '13 at 12:37
  • Hello @CoursesWeb, Please check my question updated with sample code, if you have time please try it. – San Jul 27 '13 at 12:46
  • Here is the solution: // Consider only the FORM tag if (preg_match("/[\s]*<[\s]*\bform\b[\s]*[^>]*>/", $myLine)) { // Get all the tags does not have method=post if (preg_match("/(?=^((?!method=['\"]?post['\"]?).)+$)/", $myLine)) { echo "\n". trim($myLine); } } – San Jul 28 '13 at 16:37
  • Explanation: I have used two if conditions first if() used to find the lines with FORM tag("/[\s]*<[\s]*\bform\b[\s]*[^>]*>/") second inner if() is for all the tags does NOT have method=post ("/(?=^((?!method=['\"]?post['\"]?).)+$)/") Thanks to all how tired to help me!. :-) – San Jul 28 '13 at 16:39