删除部分字符串

时间:2013-09-23 20:47:03

标签: php string

<?php
 $test = 'marke=' . $_GET["marke"] . '&farbe=' . $_GET["farbe"]. '&groesse=' . $_GET["groesse"];
?>

 <?php
echo $test;
?>
回声将是:marke = diesel&amp; farbe = blau&amp; groesse = xl

如何从$ test中删除&amp; farbe = blau?

2 个答案:

答案 0 :(得分:1)

使用parse_strhttp_build_query可靠地解析和创建查询字符串(这也会处理令人讨厌的网址编码问题):

// You could probably have created $test with:
// $test = http_build_query($_GET);
parse_str($test,$values);
unset($values['farbe']);
$test = http_build_query($values);

答案 1 :(得分:0)

您是否尝试过使用a regular expression with preg_replace()?在我的头顶,尝试类似:

preg_replace('/&farbe=.+?&/','',$test);