Solved

replace query variable

Posted on 2013-05-30
6
206 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
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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 108

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now