2012-09-07 117 views
-1

可能重复:
How to strip out a url variable如何从URL中删除字符串?

我有网址http://localhost/abc/detail.cfm?iId=1711471&countrysearch=3693&itemnumbersearch=&keywordsearch=&purl=%2FIGPCI%2Fthumbs%2Ecfm%3Fcountrysearch%3D3693%26itemnumbersearch%3D%26keywordsearch%3D%26x%3D78%26y%3D10&productid=1111&recordindex=1
我想搜索上点击链接称为“下一个”每次我需要准备的productid和recordindex.for例如新价值新的URL我有四个产品相对于countryid =“3693”产品的详细信息,

productid productname 
1   p1 
2   p2 
3   p3 
4   p4 

当下一次点击新的URL准备productid = 1和recordindex = 1时,再次单击然后URL是productid = 2并记录id = 2等等。

用于获取URL我用下面的代码:

<cfset currentURL = "#CGI.SERVER_NAME#" & "#CGI.PATH_INFO#" & "#CGI.query_string#"> 

这给我当前的URL

然后我准备新的URL与下面的代码:

<cfif queryString.recordset gt 0> <cfset recordindex=#recordindex#+1> <cfset newUrl=currentURL & '&productid=#queryString.poductid[recordindex]#&recordindex=#recordindex#' </cfif> 

与此代码,每次使用新的URL添加旧的URL值。 像:

http://localhost/abc/detail.cfm?iId=1711471&countrysearch=3693&itemnumbersearch=&keywordsearch=&purl=%2FIGPCI%2Fthumbs%2Ecfm%3Fcountrysearch%3D3693%26itemnumbersearch%3D%26keywordsearch%3D%26x%3D78%26y%3D10&productid=1111&recordindex=1&productid=2&recordindex=2 

我的问题怎么删除旧&的productid = 1111 & recordindex = 1岁URL.i与试图替换功能,但它替换字符串时正赛,在我的情况下,每一次的产品和recordindex的值是change.how使用正则表达式删除旧的字符串。请帮助我。

谢谢

回答

2

你不需要这个正则表达式。事实上,有人已经为此构建了UDF。

QueryStringDeleteVar

例子:

<cfset currentURL = CGI.SERVER_NAME & CGI.PATH_INFO & queryStringDeleteVar("productid,recordindex")> 

UDF代码:

<cfscript> 
/** 
* Deletes a var from a query string. 
* Idea for multiple args from Michael Stephenson ([email protected]) 
* 
* @param variable  A variable, or a list of variables, to delete from the query string. 
* @param qs  Query string to modify. Defaults to CGI.QUERY_STRING. 
* @return Returns a string. 
* @author Nathan Dintenfass ([email protected]@changemedia.com) 
* @version 1.1, February 24, 2002 
*/ 
function queryStringDeleteVar(variable){ 
    //var to hold the final string 
    var string = ""; 
    //vars for use in the loop, so we don't have to evaluate lists and arrays more than once 
    var ii = 1; 
    var thisVar = ""; 
    var thisIndex = ""; 
    var array = ""; 
    //if there is a second argument, use that as the query string, otherwise default to cgi.query_string 
    var qs = cgi.query_string; 
    if(arrayLen(arguments) GT 1) 
     qs = arguments[2]; 
    //put the query string into an array for easier looping 
    array = listToArray(qs,"&");   
    //now, loop over the array and rebuild the string 
    for(ii = 1; ii lte arrayLen(array); ii = ii + 1){ 
     thisIndex = array[ii]; 
     thisVar = listFirst(thisIndex,"="); 
     //if this is the var, edit it to the value, otherwise, just append 
     if(not listFind(variable,thisVar)) 
      string = listAppend(string,thisIndex,"&"); 
    } 
    //return the string 
    return string; 
} 
</cfscript> 

虽然,因为你正在寻找替换URL参数值,QueryStringChangeVar很可能会更多你想要什么。

<cfscript> 
/** 
* Changes a var in a query string. 
* 
* @param name  The name of the name/value pair you want to modify. (Required) 
* @param value  The new value for the name/value pair you want to modify. (Required) 
* @param qs  Query string to modify. Defaults to CGI.QUERY_STRING. (Optional) 
* @return Returns a string. 
* @author Nathan Dintenfass ([email protected]) 
* @version 2, September 5, 2002 
*/ 
function QueryStringChangeVar(variable,value){ 
    //var to hold the final string 
    var string = ""; 
    //vars for use in the loop, so we don't have to evaluate lists and arrays more than once 
    var ii = 1; 
    var thisVar = ""; 
    var thisIndex = ""; 
    var array = ""; 
    var changedIt = 0; 
    //if there is a third argument, use that as the query string, otherwise default to cgi.query_string 
    var qs = cgi.query_string; 
    if(arrayLen(arguments) GT 2) 
     qs = arguments[3]; 

    //put the query string into an array for easier looping 
    array = listToArray(qs,"&"); 
    //now, loop over the array and rebuild the string 
    for(ii = 1; ii lte arrayLen(array); ii = ii + 1){ 
     thisIndex = array[ii]; 
     thisVar = listFirst(thisIndex,"="); 
     //if this is the var, edit it to the value, otherwise, just append 
     if(thisVar is variable){ 
      string = listAppend(string,thisVar & "=" & value,"&"); 
      changedIt = 1; 
     } 
     else{ 
      string = listAppend(string,thisIndex,"&"); 
     } 
    } 
    //if it was not changed, add it! 
    if(NOT changedIt) 
     string = listAppend(string,variable & "=" & value,"&"); 
    //return the string 
    return string; 
} 
</cfscript>