正则请教

$str = 'si@anyunlock/download-ios.htm#pid=au#lid=year#os=mobile#brid=sf#bid=0#dl=product/index.htm#ld=anytransweb/?token=00ed9f66#/index#limitFeature=xq41tr3_50080125314367-ly42ve3_92890225314335#fromApi=true#pwdType=oderdeid#featureNo=mdm#currentFeature=mdm#coupon=MOBI62#oid=908505173'

按照k,v的方式把等号左右进行拆分 如 [‘brid’ => ‘ch’],原本直接用explode 就行,但是有个dl的值内有= 也有# 所以explode之后导致原有的值不完整,该用什么正则给匹配下,求帮助

正则请教
这一部分没匹配上,应该是 ld => anytransweb/?token=00ed9f66#/index

============================= 分割线 ========================
用这位老哥@梧桐树下给的正则 (?<=#)[^=]+=([^#]|#\/)+ 完美的匹配出来了:smiley:,谢谢大家的回复:pray:。我正则真的稀巴烂:sob:

正则请教

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 17

简单的实现方式,看下是否符合你的需求。

$result = [];
parse_str(str_replace('#', '&', $str), $result);
4个月前 评论
晏南风 (楼主) 4个月前

感觉这个应该需要先确定字段吧,不然又怎么能确定dl现在不是等于这个呢

4个月前 评论

两个思路

  1. 确定格式吧 然后匹配
  2. 把val值使用url转码一下 ['brid' => "xx#aaa=yy"] 地址就是 brid=xx%23aaa%3dyy
4个月前 评论
  • 代码

<?php
$str = 'si@anyunlock/download-ios.htm#pid=au#lid=year#os=mobile#brid=sf#bid=0#dl=product/index.htm#ld=anytransweb/?token=00ed9f66#/index#limitFeature=xq41tr3_50080125314367-ly42ve3_92890225314335#fromApi=true#pwdType=oderdeid#featureNo=mdm#currentFeature=mdm#coupon=MOBI62#oid=908505173';
$regex = '/(?:#|&)([^#=]+)=([^&#]+)/';
preg_match_all($regex, $str, $matches);
print_r($matches);
  • 结果

Array
(
    [0] => Array
        (
            [0] => #pid=au
            [1] => #lid=year
            [2] => #os=mobile
            [3] => #brid=sf
            [4] => #bid=0
            [5] => #dl=product/index.htm
            [6] => #ld=anytransweb/?token=00ed9f66
            [7] => #limitFeature=xq41tr3_50080125314367-ly42ve3_92890225314335
            [8] => #fromApi=true
            [9] => #pwdType=oderdeid
            [10] => #featureNo=mdm
            [11] => #currentFeature=mdm
            [12] => #coupon=MOBI62
            [13] => #oid=908505173
        )

    [1] => Array
        (
            [0] => pid
            [1] => lid
            [2] => os
            [3] => brid
            [4] => bid
            [5] => dl
            [6] => ld
            [7] => limitFeature
            [8] => fromApi
            [9] => pwdType
            [10] => featureNo
            [11] => currentFeature
            [12] => coupon
            [13] => oid
        )

    [2] => Array
        (
            [0] => au
            [1] => year
            [2] => mobile
            [3] => sf
            [4] => 0
            [5] => product/index.htm
            [6] => anytransweb/?token=00ed9f66
            [7] => xq41tr3_50080125314367-ly42ve3_92890225314335
            [8] => true
            [9] => oderdeid
            [10] => mdm
            [11] => mdm
            [12] => MOBI62
            [13] => 908505173
        )

)
4个月前 评论
晏南风 (楼主) 4个月前

不好意思,看下这回呢

<?php

$str = 'si@anyunlock/download-ios.htm#pid=au#lid=year#os=mobile#brid=sf#bid=0#dl=product/index.htm#ld=anytransweb/?token=00ed9f66#/index#limitFeature=xq41tr3_50080125314367-ly42ve3_92890225314335#fromApi=true#pwdType=oderdeid#featureNo=mdm#currentFeature=mdm#coupon=MOBI62#oid=908505173';

// 定义正则表达式模式
$pattern = '/#(\w+)=([^#&]+)/';

// 使用正则表达式匹配参数
preg_match_all($pattern, $str, $matches);

// 组装匹配结果为关联数组
$result = array_combine($matches[1], $matches[2]);

// 打印结果
print_r($result);

这段代码的正则表达式 #(\w+)=([^#&]+) 的解释如下:

  • #:匹配字符 “#”。
  • (\w+):捕获一个或多个单词字符,即参数的键。
  • =:匹配等号。
  • ([^#&]+):捕获一个或多个不包含 “#” 和 “&” 的字符,即参数的值。

这样,通过 preg_match_all 函数,我们可以将匹配到的键值对组合成关联数组,并打印出结果。在你提供的例子中,这段代码会输出符合你期望的关联数组。在给定的示例字符串中,使用提供的 PHP 代码,输出的结果将是如下的关联数组:

Array
(
    [pid] => au
    [lid] => year
    [os] => mobile
    [brid] => sf
    [bid] => 0
    [dl] => product/index.htm
    [ld] => anytransweb/?token=00ed9f66
    [limitFeature] => xq41tr3_50080125314367-ly42ve3_92890225314335
    [fromApi] => true
    [pwdType] => oderdeid
    [featureNo] => mdm
    [currentFeature] => mdm
    [coupon] => MOBI62
    [oid] => 908505173
)

这个关联数组包含了从字符串中提取的各个参数及其对应的值。参数的键是数组的键,而参数的值是数组的值。

4个月前 评论
晏南风 (楼主) 4个月前
VeryAUX (作者) 4个月前
晏南风 (楼主) 4个月前
<?php
$str = 'si@anyunlock/download-ios.htm#pid=au#lid=year#os=mobile#brid=sf#bid=0#dl=product/index.htm#ld=anytransweb/?token=00ed9f66#/index#limitFeature=xq41tr3_50080125314367-ly42ve3_92890225314335#fromApi=true#pwdType=oderdeid#featureNo=mdm#currentFeature=mdm#coupon=MOBI62#oid=908505173';

// 使用正则表达式匹配键值对
preg_match_all('/([^#=]+)=([^#]+(?=#|$))/', $str, $matches);

$result = array_combine($matches[1], $matches[2]);

print_r($result);
//打印结果
Array
(
    [pid] => au
    [lid] => year
    [os] => mobile
    [brid] => sf
    [bid] => 0
    [dl] => product/index.htm
    [ld] => anytransweb/?token=00ed9f66
    [limitFeature] => xq41tr3_50080125314367-ly42ve3_92890225314335
    [fromApi] => true
    [pwdType] => oderdeid
    [featureNo] => mdm
    [currentFeature] => mdm
    [coupon] => MOBI62
    [oid] => 908505173
)
4个月前 评论
晏南风 (楼主) 4个月前
#\w+=[\w\/\.]+(\?\w+=\w+\#\/\w+)*
4个月前 评论
晏南风 (楼主) 4个月前

(?<=#)[^=]+=([^#]|#\/)+

匹配内容:
共找到 14 处匹配:

pid=au
lid=year
os=mobile
brid=sf
bid=0
dl=product/index.htm
ld=anytransweb/?token=00ed9f66#/index
limitFeature=xq41tr3_50080125314367-ly42ve3_92890225314335
fromApi=true
pwdType=oderdeid
featureNo=mdm
currentFeature=mdm
coupon=MOBI62
oid=908505173

Laravel

4个月前 评论
晏南风 (楼主) 4个月前

现在正则我都是让gpt帮我写

4个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!