0

このURIを置き換えたい

http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&brand=Sony&toto=titi

このURIによって

http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&kljklj=sdfsd

==>「&brand=Sony」を削除したい

私はこれを試しました:

preg_replace('/(^.*)(&brand=.*)(&.*)/', '$1$3', 'http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&brand=Sony&toto=titi');

ただし、特定の場合には機能しません。URIにパラメータ「toto」が存在しない場合

だから私がそうするなら

preg_replace('/(^.*)(&brand=.*)(&.*)/', '$1$3', 'http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&brand=Sony'); 

動作しません==>「&brand=Sony」は引き続き表示されます

では、どうすればよいですか?

4

7 に答える 7

6

正規表現は使いません。

まず、parse_urlを使用して URL を細かく分割します。
次に、クエリ部分でparse_strを使用します。

クエリキーに対して必要なことをすべて行ってから、すべてを結合して戻します。
クエリ文字列を作成するには: http_build_query次に、 http_build_url
を使用して URL を作成します。

于 2013-03-21T17:30:43.220 に答える
0
(^.*)(&brand=.*)(&.*)?
于 2013-03-21T17:35:01.773 に答える
0
 preg_replace("/\&brand=Sony/", "", $uri);
于 2013-03-21T17:32:43.667 に答える
0

どうですか:

preg_replace('/[\?&]brand=\w*/', '', $url);
于 2013-03-21T17:33:05.083 に答える
0

キーブランドの価値を求めるなら

preg_replace('/&?brand=[^&]+/i','',$url);
于 2013-03-21T17:33:16.243 に答える
0

追加できますか?:

(^.*)(&brand=.*)(&.*)?

echo preg_replace(
'/(^.*)(&brand=.*)(&.*)?/', 
'$1$3', 
'http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&brand=Sony'); 

出力:

http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0
于 2013-03-21T17:35:47.447 に答える
0

皆さん、ありがとうございます。だからここに私の最終的な解決策があり、うまくいきます:

<?php
$actual_link = 'index.php?'.$_SERVER['QUERY_STRING']; //complete link of my page
$parsedURL= parse_url($actual_link);
parse_str($parsedURL["query"],$tabParametersQuery);    
$tabParametersQuery['brand']="";
$newURL = "index.php?".http_build_query($tabParametersQuery);
?>
于 2013-03-21T19:54:59.683 に答える