How do I remove a certain variable from a query string? Say I have a query string
$query_string = "first=val1&second=val2&third=val3";
function removevar($var, $query_string) {
return preg_replace("/(".$var."=[^&]*(&))/i","",$query_string);
}
echo removevar("first",$query_string); // ok
echo removevar("second",$query_string); // ok
echo removevar("third",$query_string); // doesn't change the string because third doesn't have a trailing &
How can this be fixed so that it removes variables from a query string in a robust way? Probably someone already has a function that does this along with special cases in more complex strings.
So I'd have to match either & or end of the string ($) but I don't know how to turn that into regex.
$query_string = "first=val1&second=val2&third=val3";
function removevar($var, $query_string) {
return preg_replace("/(".$var."=[^&]*(&|$))/i","",$query_string);
}
echo removevar("first",$query_string); // ok
echo removevar("second",$query_string); // ok
echo removevar("third",$query_string); // ok
This should do the trick.
You don’t necessarily need regular expressions for this as PHP does have functions that can parse and build query strings (parse_str and http_build_query respectively):
function removevar($var, $query_string) {
parse_str($query_string, $args);
unset($args[$var]);
return http_build_query($args);
}
Note that you need to decode the HTML character references before using these functions.
You will probably have more luck using:
html_entity_decode to get the 'normal' query-string.
parse_str to get the query string into an array.
unset the desired key in that array.
Use http_build_query to rebuild the string.
Call htmlspecialchars on it to get the & back to &.
Less concise than the regex route, but a lot less error-prone.
<?php
$query_string = "first=val1&second=val2&third=val3";
parse_str($query_string, $output);
function removevar($var, $output_array) {
if (in_array($var, $output_array)) {
unset($output_array[$var]);
}
return http_build_query($output_array, '', '&');
}
echo removevar("first", $output);
echo removevar("third", $output);
?>
Normally for codes like this, which involves a query string, it is always best to go for the in-built PHP functions rather than for the regex syntax / formula. That is what I've done, and the main parts of the code include the following PHP in-built functions:-
parse_str Function
in_array Function
http_build_query Function
Hope it helps.
function removevar($var, $query_string) {
$query_string = preg_replace("#$var=([^&]+)#is", "", $query_string);
return trim($query_string, "&");
}
There are functions, that can handle query strings directly.
function removevar ($var, $query_string) {
$array = array();
parse_str(html_entity_decode($query_string), $array);
unset($array[$var]);
return html_entities(http_build_query($array);
}
Related
Weird situation and I'm not even sure what to call it (hence unable to find any previous posts).
I'm passing in a variable that is a string as function, and then attempting to concat it to another string as such:
function saveFunction($number){
$myStr = "drawer-$number[]";
return $myStr;
}
So the output should be :
saveFunction("2")
"drawer-2[]"
However, because it thinks I am accessing $number as an array because of the brackets, the output is:
"0"
I even tried this:
function saveFunction($number){
$myStr = "drawer-$number";
return $myStr + "[]";
}
And got the same result.
Suggestions?
Double quotes in PHP are tricky. They interpret the string. This is why "drawing-$number" works in the first place.
Sometimes your string doesn't lend itself for automatic interpretation. You can use the always safe concatenation:
return "drawer-" . $number . "[]";
Or use {} to help the automatic detection:
return "drawer-{$number}[]"; // as opposed to: "drawer-{$number[]}"
Or use sprintf:
return sprintf("drawer-%s[]", $number); // %s because $number is actually a string, not an int
You can just concat the variable with the string like this:
<?php
function saveFunction($number){
return $myStr = "drawer-" . $number . "[]";
}
echo saveFunction("2");
?>
Output:
drawer-2[]
For further information see also: http://php.net/manual/en/language.operators.string.php
This seems to work ok:
function findImageTags($string) {
$pattern = '/<div(.*?)sourcefile="([^"]+)"(.*?)>(.*?)<\/div>/s';
return preg_replace($pattern, $this->generateImage("$2"), $string);
}
function generateImage($url){
return $url;
}
But when in the generateImage function I try to do something with the argument I can't because the value of the argument is $2 instead of the real value.
So this doesn't work:
function generateImage($url){
$array = explode('.', $url);
return $array;
}
by the way replacing s with e in the pattern doesn't seem to work as I think it's deprecated.
So how can I manipulate the value of the argument in generateImage() ?
What you want is probably preg_replace_callback instead of preg_replace. Here you can use a function which returns the replacement value.
The way you coded it now, the $this->generateImage("$2") code is executed at the moment you call preg_replace. It's not passed as a callback, but instead it's executed first, and the output is passed as the callback.
If you want to execute that function, you have to pass the PHP code as a string, and use the e modifier (http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php for more info).
return preg_replace($pattern, '$this->generateImage("$2")', $string);
Or use preg_replace_callback() of course.
I'm trying to give my client the ability to call a function that has various code snippets by inserted a short code in their WYSIWYG editor.
For example, they will write something like...
[getSnippet(1)]
This will call my getSnippet($id) php function and output the appropriate 'chunk'.
It works when I hard code the $id like this...
echo str_replace('[getSnippet(1)]',getSnippet(1),$rowPage['sidebar_details']);
However, I really want to make the '1' dynamic. I'm sort of on the right track with something like...
function getSnippet($id) {
if ($id == 1) {
echo "car";
}
}
$string = "This [getSnippet(1)] is a sentence.This is the next one.";
$regex = '#([getSnippet(\w)])#';
$string = preg_replace($regex, '. \1', $string);
//If you want to capture more than just periods, you can do:
echo preg_replace('#(\.|,|\?|!)(\w)#', '\1 \2', $string);
Not quite working :(
Firstly in your regex you need to add literal parentheses (the ones you have just capture \w but that will not match the parentheses themselves):
$regex = '#(\[getSnippet\((\w)\)\])#';
I also escaped the square brackets, otherwise they will open a character class. Also be aware that this captures only one character for the parameter!
But I recommend you use preg_replace_callback, with a regex like this:
function getSnippet($id) {
if ($id == 1) {
return "car";
}
}
function replaceCallback($matches) {
return getSnippet($matches[1]);
}
$string = preg_replace_callback(
'#\[getSnippet\((\w+)\)\]#',
'replaceCallback',
$string
);
Note that I changed the echo in your getSnippet to a return.
Within the callback $matches[1] will contain the first captured group, which in this case is your parameter (which now allows for multiple characters). Of course, you could also adjust you getSnippet function to read the id from the $matches array instead of redirecting through the replaceCallback.
But this approach here is slightly more flexible, as it allows you to redirect to multiple functions. Just as an example, if you changed the regex to #\[(getSnippet|otherFunction)\((\w+)\)\]# then you could find two different functions, and replaceCallback could find out the name of the function in $matches[1] and call the function with the parameter $matches[2]. Like this:
function getSnippet($id) {
...
}
function otherFunction($parameter) {
...
}
function replaceCallback($matches) {
return $matches[1]($matches[2]);
}
$string = preg_replace_callback(
'#\[(getSnippet|otherFunction)\((\w+)\)\]#',
'replaceCallback',
$string
);
It really depends on where you want to go with this. The important thing is, there is no way of processing an arbitrary parameter in a replacement without using preg_replace_callback.
I'm working with a third party API that receives several parameters which must be encoded like this:
text[]=Hello%20World&text[]=How%20are%20you?&html[]=<p>Just%20fine,%20thank%20you</p>
As you can see this API can accept multiple parameters for text, and also for HTML (not in the sample call).
I have used http_build_query to correctly build a query string for other APIs
$params['text'][] = 'Hello World';
$params['text'][] = 'How are you?';
$params['html'][] = '<p>Just fine, thank you</p>';
$http_query = http_build_query($params);
The problem with this approach is that it will build a query string with the numeric index:
text[0]=Hello%20World&text[1]=How%20are%20you?&html[0]=<p>Just%20fine,%20thank%20you</p>
unfortunately the API I'm working with doesn't like the numeric index and fails.
Is there any php function/class-method that can help me build a query like this quickly?
Thank you
I don't know a standard way to do it (I think there is no such way), but here's an ugly solution:
Since [] is encoded by http_build_query, you may generate string with indices and then replace them.
preg_replace('/(%5B)\d+(%5D=)/i', '$1$2', http_build_query($params));
I very much agree with the answer by RiaD, but you might run into some problems with this code (sorry I can't just make this a comment due to lack of rep).
First off, as far as I know http_build_query returns an urlencode()'d string, which means you won't have [ and ] but instead you'll have %5B and %5D.
Second, PHP's PCRE engine recognizes the '[' character as the beginning of a character class and not just as a simple '[' (PCRE Meta Characters). This may end up replacing ALL digits from your request with '[]'.
You'll more likely want something like this:
preg_replace('/\%5B\d+\%5D/', '%5B%5D', http_build_query($params));
In this case, you'll need to escape the % characters because those also have a special meaning. Provided you have a string with the actual brackets instead of the escapes, try this:
preg_replace('/\[\d+\]/', '[]', $http_query);
There doesn't seem to be a way to do this with http_build_query. Sorry. On the docs page though, someone has this:
function cr_post($a,$b=0,$c=0){
if (!is_array($a)) return false;
foreach ((array)$a as $k=>$v){
if ($c) $k=$b."[]"; elseif (is_int($k)) $k=$b.$k;
if (is_array($v)||is_object($v)) {
$r[]=cr_post($v,$k,1);continue;
}
$r[]=urlencode($k)."=" .urlencode($v);
}
return implode("&",$r);
}
$params['text'][] = 'Hello World';
$params['text'][] = 'How are you?';
$params['html'][] = '<p>Just fine, thank you</p>';
$str = cr_post($params);
echo $str;
I haven't tested it. If it doesn't work then you're going to have to roll your own. Maybe you can publish a github gist so other people can use it!
Try this:
$params['text'][] = 'Hello World';
$params['text'][] = 'How are you?';
$params['html'][] = '<p>Just fine, thank you</p>';
foreach ($params as $key => $value) {
foreach ($value as $key2 => $value2) {
$http_query.= $key . "[]=" . $value2 . "&";
}
}
$http_query = substr($http_query, 0, strlen($http_query)-1); // remove the last '&'
$http_query = str_replace(" ", "%20", $http_query); // manually encode spaces
echo $http_query;
$var="UseCountry=1
UseCountryDefault=1
UseState=1
UseStateDefault=1
UseLocality=1
UseLocalityDefault=1
cantidad_productos=5
expireDays=5
apikey=ABQIAAAAFHktBEXrHnX108wOdzd3aBTupK1kJuoJNBHuh0laPBvYXhjzZxR0qkeXcGC_0Dxf4UMhkR7ZNb04dQ
distancia=15
AutoCoord=1
user_add_locality=0
SaveContactForm=0
ShowVoteRating=0
Listlayout=0
WidthThumbs=100
HeightThumbs=75
WidthImage=640
HeightImage=480
ShowImagesSystem=1
ShowOrderBy=0
ShowOrderByDefault=0
ShowOrderDefault=DESC
SimbolPrice=$
PositionPrice=0
FormatPrice=0
ShowLogoAgent=1
ShowReferenceInList=1
ShowCategoryInList=1
ShowTypeInList=1
ShowAddressInList=1
ShowContactLink=1
ShowMapLink=1
ShowAddShortListLink=1
ShowViewPropertiesAgentLink=1
ThumbsInAccordion=5
WidthThumbsAccordion=100
HeightThumbsAccordion=75
ShowFeaturesInList=1
ShowAllParentCategory=0
AmountPanel=
AmountForRegistered=5
RegisteredAutoPublish=1
AmountForAuthor=5
AmountForEditor=5
AmountForPublisher=5
AmountForManager=5
AmountForAdministrator=5
AutoPublish=1
MailAdminPublish=1
DetailLayout=0
ActivarTabs=0
ActivarDescripcion=1
ActivarDetails=1
ActivarVideo=1
ActivarPanoramica=1
ActivarContactar=1
ContactMailFormat=1
ActivarReservas=1
ActivarMapa=1
ShowImagesSystemDetail=1
WidthThumbsDetail=120
HeightThumbsDetail=90
idCountryDefault=1
idStateDefault=1
ms_country=1
ms_state=1
ms_locality=1
ms_category=1
ms_Subcategory=1
ms_type=1
ms_price=1
ms_bedrooms=1
ms_bathrooms=1
ms_parking=1
ShowTextSearch=1
minprice=
maxprice=
ms_catradius=1
idcatradius1=
idcatradius2=
ShowTotalResult=1
md_country=1
md_state=1
md_locality=1
md_category=1
md_type=1
showComments=0
useComment2=0
useComment3=0
useComment4=0
useComment5=0
AmountMonthsCalendar=3
StartYearCalendar=2009
StartMonthCalendar=1
PeriodOnlyWeeks=0
PeriodAmount=3
PeriodStartDay=1
apikey=ABQIAAAAJ879Hg7OSEKVrRKc2YHjixSmyv5A3ewe40XW2YiIN-ybtu7KLRQiVUIEW3WsL8vOtIeTFIVUXDOAcQ
";
in that string only i want "api==ABQIAAAAJ879Hg7OSEKVrRKc2YHjixSmyv5A3ewe40XW2YiIN-ybtu7KLRQiVUIEW3WsL8vOtIeTFIVUXDOAcQ";
plz guide me correctly;
EDIT
As shamittomar pointed out, the parse_str will not work for this situation, posted the proper regex below.
Given this seems to be a QUERY STRING, use the parse_str() function PHP provides.
UPDATE
If you want to do it with regex using preg_match() as powertieke pointed out:
preg_match('/apikey=(.*)/', $var, $matches);
echo $matches[1];
Should do the trick.
preg_match(); should be right up your alley
people are so fast to jump to preg match when this can be done with regular string functions thats faster.
$string = '
expireDays=5
apikey=ABQIAAAAFHktBEXrHnX108wOdzd3aBTupK1kJuoJNBHuh0laPBvYXhjzZxR0qkeXcGC_0Dxf4UMhkR7ZNb04dQ
distancia=15
AutoCoord=1';
//test to see what type of line break it is and explode by that.
$parts = (strstr($string,"\r\n") ? explode("\r\n",$string) : explode("\n",$string));
$data = array();
foreach($parts as $part)
{
$sub = explode("=",trim($part));
if(!empty($sub[0]) || !empty($sub[1]))
{
$data[$sub[0]] = $sub[1];
}
}
and use $data['apikey'] for your api key, i would also advise you to wrpa in function.
I can bet this is a better way to parse the string and much faster.
function ParsemyString($string)
{
$parts = (strstr($string,"\r\n") ? explode("\r\n",$string) : explode("\n",$string));
$data = array();
foreach($parts as $part)
{
$sub = explode("=",trim($part));
if(!empty($sub[0]) || !empty($sub[1]))
{
$data[$sub[0]] = $sub[1];
}
}
return $data;
}
$data = ParsemyString($string);
First of all, you are not looking for
api==ABQIAAAAJ879Hg7OSEKVrRKc2YHjixSmyv5A3ewe40XW2YiIN-ybtu7KLRQiVUIEW3WsL8vOtIeTFIVUXDOAcQ
but you are looking for
apikey=ABQIAAAAJ879Hg7OSEKVrRKc2YHjixSmyv5A3ewe40XW2YiIN-ybtu7KLRQiVUIEW3WsL8vOtIeTFIVUXDOAcQ
It is important to know if the api-key property always occurs at the end and if the length of the api-key value is always the same. I this is the case you could use the PHP substr() function which would be easiest.
If not you would most probably need a regular expression which you can feed to PHPs preg_match() function. Something along the lines of apikey==[a-zA-Z0-9\-] Which matches an api-key containing a-z in both lowercase and uppercase and also allows for dashes in the key. If you are using the preg_match() function you can retrieve the matches (and thus your api-key value).