使用 preg_match 从一个完整的句子中提取一些信息
extract some info from a complete sentance using preg_match
我想在 php 中使用 preg_match 提取特定信息。有什么想法吗?
sample text = 2xMUA Matte Lipstick - Totally Nude[300]=[600]
这里我要
product_name = MUA Matte Lipstick - Totally Nude
product_qty = 2
product_price = 300
product_subtotal = 600
您可以在(命名)captured groups.
中捕捉您的价值观
这样试试:
(?<qty>\d+)x(?<name>[^[]+)\[(?<price>[^]]+)\]=\[(?<subtotal>[^]]+)\]
那将匹配:
(?<qty> # Named captured group qty
\d+ # One or more digits
) # Close group
x # Match x
(?<name> # Named captured group name
[^[]+ # Match not [ one or more times
) # Close group
\[ # Match [
(?<price> # Named captured group price
[^]]+ # Match not ] one or more times
) # Close group
\]=\[ # Match ]=[
(?<subtotal> # Named captured group subtotal
[^]]+ # # Match not ] one or more times
) # Close group
\] # Match ]
或没有命名的捕获组:
我想在 php 中使用 preg_match 提取特定信息。有什么想法吗?
sample text = 2xMUA Matte Lipstick - Totally Nude[300]=[600]
这里我要
product_name = MUA Matte Lipstick - Totally Nude
product_qty = 2
product_price = 300
product_subtotal = 600
您可以在(命名)captured groups.
中捕捉您的价值观这样试试:
(?<qty>\d+)x(?<name>[^[]+)\[(?<price>[^]]+)\]=\[(?<subtotal>[^]]+)\]
那将匹配:
(?<qty> # Named captured group qty \d+ # One or more digits ) # Close group x # Match x (?<name> # Named captured group name [^[]+ # Match not [ one or more times ) # Close group \[ # Match [ (?<price> # Named captured group price [^]]+ # Match not ] one or more times ) # Close group \]=\[ # Match ]=[ (?<subtotal> # Named captured group subtotal [^]]+ # # Match not ] one or more times ) # Close group \] # Match ]
或没有命名的捕获组: