【问题标题】:what is the regex for url's with parameters?带有参数的 url 的正则表达式是什么?
【发布时间】:2012-08-28 23:55:29
【问题描述】:

如果我有一个<form action='./search' method='get'>
和<input type='text' name='keyword' value='this is the search term' />inside

提交表单后,它引导我到/search?keyword=this%20is%20the%search%20term

我应该使用什么正则表达式来捕获这种形式的 url 并将其重写为
/search-this_is_the_search_term

【问题讨论】:

    标签: php regex url-rewriting


    【解决方案1】:

    首先urldecode 搜索字符串,然后用下划线替换空格。例如:

    $str = "this%20is%20the%search%20term";
    $decoded = urldecode($str);
    $final = str_replace(' ', '_', $decoded);
    

    您可以更可靠地使用 preg_replace 对正则表达式执行相同操作,因为您可能需要删除多个连续空格:

    $str = "this%20is%20the%search%20term";
    $decoded = urldecode($str);
    $final = preg_replace("/\s+/", "_", $decoded);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-31
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-16
      • 2011-06-15
      相关资源
      最近更新 更多