PHP Regex pattern across a line
I have:
action=wuffwuff
form
action=meowmeow
action=mooomooo
How can I extract the value of action after "开发者_StackOverflowform"?
Using preg_match, I tried the pattern
/form.*?action=(.*)/m
Which somehow doesn't work
Thank you
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
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"
}
精彩评论