0

I have:

action=wuffwuff
form
action=meowmeow
action=mooomooo

How can I extract the value of action after "form"?

Using preg_match, I tried the pattern

/form.*?action=(.*)/m

Which somehow doesn't work

Thank you

moogle
  • 1
  • What kind of data is this? Do you control the way it is stored? You could use the INI file format for example, PHP has a built-in parser for that – Pekka Aug 21 '11 at 09:42
  • it is a string in a variable, I don't have control over this. I'm trying to parse data from curl. I simplified it so it's easier to understand – moogle Aug 21 '11 at 09:48
  • Robus, I want to extract just a single value from the HTML page, I believe this way will be much more efficient – moogle Aug 21 '11 at 10:06

2 Answers2

2

You do not have to use the multi line modifier //m but instead //s such that your first dot matches everything including the newline (you can read about the meaning of the modifiers here).

Additionally you should restrict your group to everything non-newline:

/form.*?action=([^\n]*)/s
Howard
  • 38,639
  • 9
  • 64
  • 83
0

If you want the single action right after form:

preg_match( '/form.*?action=(.*?)\r?(?:\n|$)/s', $str, $matches );
var_dump( $matches[1] );

result:

string(8) "meowmeow"

If you want to get all actions after form with a preg_match_all() you can't do that because that requires a variable length lookbehind like (?<=form.*) which is not allowed in PHP. But if you remove the "after form" requirement (split the string and just keep the part after form) you can use this regex to get all actions:

preg_match_all( '/action=(.*?)\r?(?:\n|$)/s', $str, $matches );
var_dump( $matches[1] );

result:

array(3) {
  [0]=>
  string(8) "wuffwuff"
  [1]=>
  string(8) "meowmeow"
  [2]=>
  string(8) "mooomooo"
}
nobody
  • 10,599
  • 4
  • 26
  • 43