Solved

replace query variable

Posted on 2013-05-30
6
210 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 167 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
Technology Partners: 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 167 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 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 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

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!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

756 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