Solved

replace query variable

Posted on 2013-05-30
6
211 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

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!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

737 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