簡體   English   中英

如何在 PHP 中使用與整個字符串中的給定組匹配的正則表達式,而不是在第一次匹配時停止

[英]How can i use a regular expression in PHP that matches a given group in the whole string instead of stops at first match

我有以下表達式:

[\?&]([\w-]+)=([\w=.|()+%-]+)

我正在嘗試驗證以下查詢字符串:

?tn_cid=280&tn_fk_specifications_planten_planttype=Groene%20kamer+planten|hangplanten|tuinplanten&tn_fk_ae-waterbehoefte=Weinig%20(2x%20per%20maand)&tn_fk_specifications_planten_plantheight=30%20-%2035%20cm&tn_fk_ae-hoogte-plant=Kleine%20planten%20(tot%2040%20cm)&tn_fk_ae-prijs=16.99-16.99

但我的表達式只匹配字符串的第一部分,然后停止:

?tn_cid=280

如何讓表達式繼續匹配每個組(即?key=value 或 &key=value)直到完整字符串的結尾?

您可以重復整個表達式,並且可以省略捕獲組以獲得完整匹配:

(?:[?&][\w-]+=[\w=.|()+%-]+)+

正則表達式演示

還是以? 並可選擇用&重復該部分

\?[\w-]+=[\w.|()+%-]+(?:&[\w-]+=[\w.|()+%-]+)*

正則表達式演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM