0

How can I replace a certain part of a string. For example we have the URL:

username=[LINK]&quantity=10&limit=[POSTS]&interval=5&url=https://google.com/&service=762&runs=[RUNS]&type=Comments

I want to keep the parameters, quantity=\, limit=, interval=* & runs=*.

I've tried to do it with parsing but can't get it work,

 parse_str($p_api, $query);                                   
 $quantity = '&quantity='.$query['quantity'];
 $limit = '&limit='.$query['limit'];
 $interval = '&interval='.$query['interval'];
 $runs = '&runs='.$query['runs'];

How can I update all other data but keep these parameters as they are in the default string?

Nick
  • 138,499
  • 22
  • 57
  • 95
  • What does `quantity=\, limit=, interval=* & runs=*` mean? – Nick Nov 17 '18 at 00:36
  • 2
    Possible duplicate of [Parse query string into an array](https://stackoverflow.com/questions/5397726/parse-query-string-into-an-array) – Markus Safar Nov 17 '18 at 00:37
  • They are just variables, for example: quantity=15, interval=5... the point is to keep their values but everything else to be changed. –  Nov 17 '18 at 00:37
  • `"can't get parse_str to work`" do you see any error messages or do you have error reporting enabled? –  Nov 17 '18 at 00:50
  • No, i mean the logic with parse_str i can't get it to work because i am not sure how can i replace only certain parts of the string. –  Nov 17 '18 at 02:41

1 Answers1

0

you can parse query parse_str(), update array value you wish and http_build_query() for url encoding

$str = "username=[LINK]&quantity=10&limit=[POSTS]&interval=5&url=https://google.com/&service=762&runs=[RUNS]&type=Comments";
  parse_str($str,$query);
  $query['username']  = "xyz";
  $query['type'] = "post";
  echo http_build_query($query);

Demo

suresh bambhaniya
  • 1,687
  • 1
  • 10
  • 20