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

3
  • 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 Commented Aug 21, 2011 at 9: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 Commented Aug 21, 2011 at 9:48
  • Robus, I want to extract just a single value from the HTML page, I believe this way will be much more efficient Commented Aug 21, 2011 at 10:06

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.