better way to replace query string value in a given url

前端 未结 8 2017
南方客
南方客 2020-12-11 16:32

Okay.. so basically, say we have a link:

$url = \"http://www.site.com/index.php?sub=Mawson&state=QLD&cat=4&page=2&sort=z\";

8条回答
  •  死守一世寂寞
    2020-12-11 17:24

    If I'm reading this correctly, and I may not be. You know which GET you are replacing in a url string? This may be sloppy but...

    $url_pieces = explode( '?', $url );
    $var_string = $url_pieces[1].'&';
    $new_url = $url_pieces[0].preg_replace( '/varName\=value/', 'newVarName=newValue', $var_string );
    

    That's my take, Good luck.

提交回复
热议问题