Mubin
Mubin

Reputation: 4425

php preg_match is not returning expected results

I have this

$str = '"javascript:OpenWindow("order.aspx?order_id=161FA084AEF13FD7")"';
preg_match('/order\.aspx\?order_id=(.*\"\))/', $str, $a);
print_r($a);

output expecting:

161FA084AEF13FD7

but getting

161FA084AEF13FD7")

Please improve this..

Upvotes: 0

Views: 62

Answers (2)

Blah Argh
Blah Argh

Reputation: 302

As another solution, you only really need to move your closing capture parenthesis over so it doesn't include the quote and the closing parenthesis characters.

As in, use this:

/order\.aspx\?order_id=(.*)\"\)/
                          ^
                          |
                   move this over here

Upvotes: 1

anubhava
anubhava

Reputation: 785176

Try this regex:

preg_match('/order\.aspx\?order_id=([^")]+)/', $str, $a);

[^")]+ is negation based regex that matches text until a " OR ) is found thus matching identifier before " or ) without actually capturing it.

Upvotes: 2

Related Questions