0

4 hours attempting to learn regex and I am always getting an error. I want get action value for cURL.

<form id="loginForm" name="loginForm" method="post" action="M_Username_Password.aspx?__ufps=802858&amp;84E09046BECF819E=6C1143C04AF5072F1DF5B1C51C90DACB">

Take this part from the form tag:

M_Username_Password.aspx?__ufps=802858&amp;84E09046BECF819E=6C1143C04AF5072F1DF5B1C51C90DACB

And use it with cURL.

curl_setopt($ch, CURLOPT_URL, "http://website.com/$linkaction");

$linkaction is an example.

kulme
  • 1

1 Answers1

0

Regular expression:

<form.*?action="([^"]*)".*?>

Combined with preg_match_all():

$html = '<form id="loginForm" name="loginForm" method="post" action="M_Username_Password.aspx?__ufps=802858&amp;84E09046BECF819E=6C1143C04AF5072F1DF5B1C51C90DACB">'; // from cURL
preg_match_all('/<form.*?action="([^"]*)".*?>/i', $html, $matches);

var_dump($matches[1]); // An array of form actions
// array(1) {
//   [0]=>
//   string(92) "M_Username_Password.aspx?__ufps=802858&84E09046BECF819E=6C1143C04AF5072F1DF5B1C51C90DACB"
// }

But in the end, don't rely on regex to parse HTML. Try a DOM parser like DOMDocument.

Community
  • 1
  • 1
Sam
  • 20,096
  • 2
  • 45
  • 71