sunhk25
V2EX  ›  问与答

正则修改 url 参数值

  •  1
     
  •   sunhk25 · Dec 1, 2016 · 1647 views
    This topic created in 3465 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
        1
    SilentDepth  
       Dec 1, 2016   ❤️ 1
    /&([a-z]+)(?:=([^&]+)?)?/i
    这样?
    whahugao
        2
    whahugao  
       Dec 1, 2016   ❤️ 1
    ```php
    $s = '/tst/req.php?szID=123&tst=&szPass=s123&szLimit=20&szInf=test';
    $ss = preg_replace('/([\?&][a-zA-Z]+)([^&]*)/','\1\2A',$s);
    ```
    sunhk25
        3
    sunhk25  
    OP
       Dec 1, 2016
    @whahuzhihao 谢谢,可以的
    mooncakejs
        4
    mooncakejs  
       Dec 1, 2016   ❤️ 1
    php 的话不如 parse_url 何必这么辛苦
    sunhk25
        5
    sunhk25  
    OP
       Dec 1, 2016
    @mooncakejs 这个挺方便的,我用的是 c#,查了下有这个函数 ParseQueryString 跟 parse_url 差不多
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1165 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 18:01 · PVG 02:01 · LAX 11:01 · JFK 14:01
    ♥ Do have faith in what you're doing.