sunhk25
V2EX  ›  问与答

正则修改 url 参数值

  •  1
     
  •   sunhk25 · Dec 1, 2016 · 1975 views
    This topic created in 3626 days ago, the information mentioned may be changed or developed.
    有 url=> /tst/req.php?szID=123&tst=&szPass=s123&szLimit=20&szInf=test
    参数都有可能为空,想要改为例如:
    /tst/req.php?szID=123A&tst=A&szPass=s123A&szLimit=20A&szInf=testA
    想用正则表达式修改参数值,前方参照([\?&][a-zA-Z]+?\=)后,参数值不知如何取
    5 replies  •  2016-12-01 11:07:18 +08:00
    SilentDepth
    SilentDepth
    Dec 1, 2016❤️1
    1
    /&([a-z]+)(?:=([^&]+)?)?/i
    这样?
    whahugao
    whahugao
    Dec 1, 2016❤️1
    2
    ```php
    $s = '/tst/req.php?szID=123&tst=&szPass=s123&szLimit=20&szInf=test';
    $ss = preg_replace('/([\?&][a-zA-Z]+)([^&]*)/','\1\2A',$s);
    ```
    sunhk25
    sunhk25
    OP
    Dec 1, 2016
    3
    @whahuzhihao 谢谢,可以的
    mooncakejs
    mooncakejs
    Dec 1, 2016❤️1
    4
    php 的话不如 parse_url 何必这么辛苦
    sunhk25
    sunhk25
    OP
    Dec 1, 2016
    5
    @mooncakejs 这个挺方便的,我用的是 c#,查了下有这个函数 ParseQueryString 跟 parse_url 差不多
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1297 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 21:21 · PVG 05:21 · LAX 13:21 · JFK 16:21
    ♥ Do have faith in what you're doing.