Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preg_match and arrays 1

Status
Not open for further replies.

jasc2k

Programmer
Nov 2, 2005
113
GB
Hi all,

Below is a function I have wrote for remembering previous URL vars that have been posted. i.e I have a mail section on my site where you can sort by different things like date or subject. they can also choose to sort asc or desc - my function will remember their previous choice and/or update it accordingly.

If anyone has any suggestions code samples to improve this please post them but in short my issue lies with my preg_match statement. Some vars sent via the URL are not wanted to be remembered i.e delete (for obvious reasons.)

I am attempting to preg_match multiple regex's but I believe its wrong (well it does not work :eek:)). I have used the /blah/ operators as this may only be a partial match on one side.

Code:
/**
    * remURL - this little function will
    * remember any url and update new vars
    */
 function remURL($var,$value){
  //$url = '';
  $i=0;
  $url = $_SERVER['REQUEST_URI'];
  $arr_var = explode(',', $var);
  $arr_value = explode(',', $value);
  
 //SOMETHING IS WRONG IN HERE - preg_match wrong???? post in a forum????
  /* Firstly cleanse the URL for any vars not wanted to be reposted */
 //if($url != ''){
  $urlpieces = explode('&', $url);
  foreach($urlpieces as $key => $val) {
   /* Dont remember certain vars in URL (separator |) */
   //if(preg_match("/=delete/", $val) || preg_match("/=markread/", $val) || preg_match("/=markunread/", $val) || preg_match("/pstid=/", $val) || preg_match("/msgid=/", $val)) {
   if(preg_match("(/=delete/|/=markread/|/=markunread/|/pstid=/|/msgid=/)", $val)){
    /* If the var is the one being posted remember it this time */
    $pos = strpos($val, $arr_var[$key]);
    if($pos === false){ unset($urlpieces[$key]); 
    echo "unset :".$urlpieces[$key]; }
   }
  }
  /* Create new array and put back into a string */
  $new_url = array_values($urlpieces);
  $url = implode("&",$new_url);
 //}
  
  
  

  while($i < count($arr_var)){ 
   if(isset($_GET[$var[$i]]) && $_GET[$var[$i]] != ""){
    /* Reply existing url with var modified */
    //$url = $_SERVER['REQUEST_URI'];
    if($arr_value[$i] != ''){
     $url = str_replace($arr_var[$i]."=".$_GET[$arr_var[$i]],$arr_var[$i]."=".$arr_value[$i],$url);
    } else {
     /* This will forget the var */
     $url = str_replace("&".$arr_var[$i]."=".$_GET[$arr_var[$i]],'',$url);
    }
   } else {
    /* Reply existing url with new var - remember to remove slash */
    if($arr_value[$i] != ''){ 
     $url .= "&".$arr_var[$i]."=".$arr_value[$i];
 //echo $url;  //substr_replace(BASE_URL,"",-1).$_SERVER['REQUEST_URI'].
     
    } else { // is this needed???????????
     $url = substr_replace(BASE_URL,"",-1).$_SERVER['REQUEST_URI'];
    }
   }
  $i++;
  } 
  return $url;
 }

 

 

usage:

<a class='deletesure' href='".$database->remURL('action,pstid',"delete,".$list['post_id'])."'>

Hope this makes sense to someone,
Thanks in advance
James
 
sure. both can have separate nonces because only one form can be submitted.
 
ideal - have not tried yet still ensuring the code is site wide.
Thank you very much for your help you have been great!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top