php 正则匹配 变量名称,php – 正则表达式匹配名称和可选值

我发现的最简单的解决方案(到目前为止)是这样的:

(\w+) : ?(.*?)(?= ?\w+ :|$)

最后,添加?最后,正如艾伦所建议的那样,输出更加出色.

(\w+) : ?(.*?)(?= ?\w+ :|$) ?

输出:

[0] => Array

(

[0] => bypass_first_page :

[1] => x_company :

[2] => x_cust_id : 12345

[3] => x_customer_ip :

[4] => x_customer_tax_id :

[5] => x_description : 98765

[6] => x_duty :

[7] => x_email_customer : an_example@example.com

[8] => x_fax :

[9] => x_footer_email_receipt :

[10] => x_fp_hash : 747ffeddfe4e106a9c67363ebff996ad

[11] => x_fp_timestamp : 1525100766

[12] => x_invoice_num : R000098765

[13] => x_login : MY-LOGIN-ID

[14] => x_logo_url :

[15] => x_merchant_email :

[16] => x_method :

[17] => x_phone : (416) 555-1212

[18] => x_po_num :

[19] => x_receipt_link_method : GET

[20] => x_reference_3 : 1234

[21] => x_relay_response : TRUE

[22] => x_relay_url :

)

[1] => Array

(

[0] => bypass_first_page

[1] => x_company

[2] => x_cust_id

[3] => x_customer_ip

[4] => x_customer_tax_id

[5] => x_description

[6] => x_duty

[7] => x_email_customer

[8] => x_fax

[9] => x_footer_email_receipt

[10] => x_fp_hash

[11] => x_fp_timestamp

[12] => x_invoice_num

[13] => x_login

[14] => x_logo_url

[15] => x_merchant_email

[16] => x_method

[17] => x_phone

[18] => x_po_num

[19] => x_receipt_link_method

[20] => x_reference_3

[21] => x_relay_response

[22] => x_relay_url

)

[2] => Array

(

[0] =>

[1] =>

[2] => 12345

[3] =>

[4] =>

[5] => 98765

[6] =>

[7] => an_example@example.com

[8] =>

[9] =>

[10] => 747ffeddfe4e106a9c67363ebff996ad

[11] => 1525100766

[12] => R000098765

[13] => MY-LOGIN-ID

[14] =>

[15] =>

[16] =>

[17] => (416) 555-1212

[18] =>

[19] => GET

[20] => 1234

[21] => TRUE

[22] =>

)

我做了一些测试,认为这应该符合要求.

PS:我提出的第一个解决方案是this:

(?:^| )(\w+) : ?(?!\w+ : )(?:(.*?)(?= \w+ :|$))?

它有点冗长,但也可能对你有所帮助.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值