Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

replace query variable

Posted on 2013-05-30
6
Medium Priority
?
214 Views
Last Modified: 2013-06-02
replace
$replace_query_string with ''

website.com/index.php?query_variable1=45&query_variable2=59499


if $replace_query_string='query_variable1'
want  
website.com/index.php?query_variable2=59499

if $replace_query_string='query_variable2'
want  
website.com/index.php?query_variable1=45&
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39209888
Try this:

$query_string = preg_replace('#'.preg_quote($replace_query_string,'#').'=[^&]*&?#', '', $query_string);

Open in new window


I haven't tested it; let me know if you have any trouble and I'll take another look.
0
 

Author Comment

by:rgb192
ID: 39209897
<?php
$query_string='website.com/index.php?query_variable1=45&query_variable2=59499';
  $query_string = preg_replace('#'.preg_quote($replace_query_string,'#').'=[^&]*&?#', '', $query_string);
  echo $query_string;

Open in new window


website.com/index.php?query_variable1query_variable2

want to delete one query_string variable and one query_string value
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 668 total points
ID: 39209902
Is the value of $replace_query_string populated?

Here's a test of the different possible values stated:
<?
$query_string='website.com/index.php?query_variable1=45&query_variable2=59499';
$replace_query_strings = array(
        'query_variable1',
        'query_variable2'
);

print "Before:\n$query_string\n";
foreach ($replace_query_strings as $replace_query_string) {
 print "When replace_query_string is: $replace_query_string\n";
 print preg_replace('#'.preg_quote($replace_query_string,'#').'=[^&]*&?#', '', $query_string);
 print "\n";
}

Open in new window

Output:
Before:
website.com/index.php?query_variable1=45&query_variable2=59499
When replace_query_string is: query_variable1
website.com/index.php?query_variable2=59499
When replace_query_string is: query_variable2
website.com/index.php?query_variable1=45&

Open in new window

ie it does give the intended output
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Assisted Solution

by:Jagadishwor Dulal
Jagadishwor Dulal earned 668 total points
ID: 39210023
$query_string='website.com/index.php?query_variable1=45&query_variable2=59499';
  $result=explode('&', $query_string);
echo $result[0];

Open in new window

0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 664 total points
ID: 39210520
There must be some context to this question, because in its isolated form, it looks like an exercise in using the if() statement and the assignment operator, and everyone who has had more than one hour of PHP instruction knows how to do that.  Fly up a little higher and look down on the larger problem.  Tell us what you see in the central issue and the surrounding landscape.  We can probably identify and explain a commonly used design pattern -- something that covers anchor tags and URL arguments, maybe.
0
 

Author Closing Comment

by:rgb192
ID: 39213967
first two solutions worked,
explode & simple and effective
next solution was me realizing different design pattern

thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question