How to replace a specific query string value of a url through PHP using an array?

Asked

Viewed 464 times

1

I have the following url that is returned to me by a webservice in a certain part of my application. But it comes with a query string that determines the size of the image (according to the parameter width this url brings me the image with different size).

I wish I could replace only this query string value, the width, and replace by another.

I want to do it from a array. I mean, I’m gonna have one array with the values I want to replace in the query string of that url, but I cannot remove the others.

For example. If I have this url:

 http://site.com/images/?sistema=3&size=500&type=png;

I want to transform it, transforming the value of 500 for 200, making it so:

 http://site.com/images/?sistema=3&size=200&type=png;

How I want to do through a array, I need something like this:

 // retornado pelo webservice

 $url = 'http://site.com/images/?sistema=3&size=500&type=png';

 $url = transforma_query_string($url, array('size' => 200));

That would have to come back:

'http://site.com/images/?sistema=3&size=200&type=png;'

How could I create this function in PHP? Can anyone help me?

  • how is your array?

  • Look at the last example... array('size' => 200)... I already asked the question :)

  • 2

    Wouldn’t it be easier to use one str_replace no? That way: $size = 200; $url = 'http://site.com/images/?sistema=3&size=500&type=png'; and at printar time put so $url = str_replace( '&size=500', '&size='.$size, transforma_query_string($url) );

2 answers

2


Specific option

function transforma_query_string($str, $parameters) {
    // considerando que a url será sempre size=500 e que outros parâmetros nunca terão o valor 500:
    return str_replace('500', $parameters['size'], $str);
}

$url = 'http://site.com/images/?sistema=3&size=500&type=png';
echo transforma_query_string($url, array('size' => 200));

Generic option

Something more generic, where you can use for different Urls:

function foo($url, $new_query) {
    $arr = parse_url($url);
    parse_str($arr['query'], $query);
    return $arr['scheme'].'://'.$arr['host'].$arr['path'].'?'.http_build_query(array_replace($query, $new_query));
}

$url = 'http://site.com/images/?sistema=3&size=500&type=png';
echo foo($url, array('size' => 200));
  • 2

    To create something reusable, I hate repeating code...

  • 1

    got it.. then you want something generic that can modify any parameter of a query string, regardless of the url?

  • 1

    updated........

  • That’s right, thanks :D

1

function transforma_query_string($url,$dados){
   $url=explode('?',$url,2);//separa o link dos argumentos
   $link=$url[0].'?';//adiciona o ? para ficar pronto para os argumentos
   $argumentos=explode('&',$url[1]);//divide os argumentos
   foreach($argumentos AS $i){//corre todos os argumentos
       $temp=explode('=',$i);//separa o index e valor
       if (array_key_exists($temp[0],$dados)){//verifica se é para alterar
           $temp[1]=$dados[$temp[0]];//altera
       }
       $link.=$link==$url[0].'?'?$temp[0].'='.$temp[1]:'&'.$temp[0].'='.$temp[1];//Adiciona ao link
   }
   return $link;// envia o link alterado
}
  • Good answer. I just don’t understand why you didn’t use http_build_query. I thought the foreach was unnecessary, not to mention that you would have to check the case where the url has an array of elements, as in the case of x[]=1&x[]=2,

Browser other questions tagged

You are not signed in. Login or sign up in order to post.