Solved

replace query variable

Posted on 2013-05-30
6
212 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
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.

635 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