简体   繁体   中英

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..

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM