如何从表单操作中获取URL? 喜欢在php中解压缩或解析[关闭]

I want to get URL from HTML form action. How can I get this in PHP? The form tag like

<form method="post" action="my.aspx? id="aspnetForm" enctype="multipart/form-data">

</form>

You can try to load the HTML with DOM and then extract the information using xpath:

// create a DOM document object and load your html
$doc = new DOMDocument();
$doc->loadHtml($yourHtml);

// create an Xpath selector for the document
$selector = new DOMXpath($doc);

// the following query will return all <form> elements
// you may refine it
$result = $selector->query('//form');

// get the first item (to keep the example simple)
$form = $result->item(0);

// get the value of the action attribute
$url = $form->getAttribute('action');

You can use regexp. But I think for you will simple make next steps:

  1. with stristr function detect begin of form tag - search string "<form"
  2. use stristr again for find begin of string "action="
  3. use substr for truncate first 7 chars to begin url string
  4. use strpos for detect first space after url string
  5. use substr for get string of url
  6. delete space and quotes from this string. Use for this str_replace or trim function

I think this will help you for study how you can parse any html tags and others things.

you can get like this..

      <?php
       $string = <<<XML
       <form method="post" action="my.aspx? id="aspnetForm" enctype="multipart/form-data"></form>
       XML;

      $xml = new SimpleXMLElement($string);

      $att = 'action';
      $attribute = $xml->form[0]->attributes()->$att; // answer
      ?>