dongpa3109 2014-04-09 19:23
浏览 45
已采纳

需要帮助为preg_replace编写正则表达式

Well, I want to extract parameters from url using preg_replace. For now I have something like this:

preg_replace("%(?<=[\?&])$param=.+(&|$)%", '', $url);

But it doesn't work well. The problem is with the ending of expression. How can I write expression that would do the following: "match & character or the end of the string"?

  • 写回答

1条回答 默认 最新

  • dsvyc66464 2014-04-10 21:55
    关注

    I'm a little confused by your post as you are saying that you want to "extract" the parameters, but then you say you want to use preg_replace. If you want to actually extract the parameters so you can process them, you'd probably want preg_match_all instead. If want to replace the question mark and everything after it with nothing, (which is what I think you're trying to do from your sample code) that'd be pretty easy to accomplish. Something like this would do that:

    $string = 'https://www.google.com/search?q=Metallica+Turn+The+Page&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&channel=sb';
    
    $string = preg_replace('~\?.*~', '', $string);
    
    print $string;
    

    This looks for a question mark \?, followed by any character ., any number of times * and then replaces it with nothing. This will give you:

    https://www.google.com/search
    

    If you want to actually replace each parameter with something, you can do that as well

    // REPLACE ALL OF THE PARAMS WITH DUMMY-PARAM
    $string = 'https://www.google.com/search?q=Metallica+Turn+The+Page&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&channel=sb';
    
    $string = preg_replace('~[?&]\K([A-Z0-9+%]+)=.+?(?=&|$)~i', '$1=DUMMY-PARAM', $string);
    
    print "
    
    ".$string;
    

    Or

    // REPLACE ALL OF THE KEYS WITH DUMMY-KEY
    $string = 'https://www.google.com/search?q=Metallica+Turn+The+Page&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&channel=sb';
    
    $string = preg_replace('~[?&]\K[A-Z0-9+%]+=(.+?)(?=&|$)~i', 'DUMMY-KEY=$1', $string);
    
    print "
    
    ".$string;
    

    Or

    // PULL OUT ALL KEYS/VALUES
    $string = 'https://www.google.com/search?q=Metallica+Turn+The+Page&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&channel=sb';
    
    $string = preg_replace('~[?&]\K([A-Z0-9+%]+)=(.+?)(?=&|$)~i', "
    $1: $2", $string);
    
    print "
    
    ".$string;
    

    All three of these start off with a character class looking for a question mark or ampersand. The \K tells the expression to start matching once it finds whatever was before it. Then we look for the key which is a character class of some of the URL characters, followed by an equal sign and followed by at least one character (but can be more), all the way up until it hits the next part of the expression. Here, I've used .+? to represent that, but it would be even better to use a character class instead. Something like this worked for this expression: ([-.:A-Z0-9+%]+)?. In any case, the next part of the expression is a lookahead to check and see if an ampersand or the end of the line is coming up next.

    Here is a working demo for you to compare the differences

    Hopefully that answers your question properly. As I mentioned earlier, I was confused by it. If not, let me know and I will reevaluate.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集