删除PHP中特定字符串开头的字符串

删除PHP中特定字符串开头的字符串,php,regex,string,Php,Regex,String,我在php中有这样一个url字符串: $string = 'test.xyz/builder-list/?price=301-500%2C501-1000&builder_country=6442%2C6780%2C6441'; 我想从以“%2c”开头的URL字符串中的price查询值中删除特定字符串。例如: test.xyz/builder-list/?price=301-500%2C501-1000&builder_country=6442%2C6780%2C6441 in

我在php中有这样一个url字符串:

$string = 'test.xyz/builder-list/?price=301-500%2C501-1000&builder_country=6442%2C6780%2C6441';
我想从以“%2c”开头的URL字符串中的
price
查询值中删除特定字符串。例如:

test.xyz/builder-list/?price=301-500%2C501-1000&builder_country=6442%2C6780%2C6441
into
test.xyz/builder-list/?price=301-500&builder_country=6442%2C6780%2C6441

test.xyz/builder-list/?price=-200%2C400-500&builder_region=1223%2C3445
into
test.xyz/builder-list/?price=-200&builder_region=12%2C33

test.xyz/builder-list/?builder_state=45%2C76&price=-200%2C400-500
into
test.xyz/builder-list/?builder_state=45%2C76&price=-200
我试图使用这个
preg\u replace
函数,但它删除了所有
%2C
字符串

preg_replace('/' . preg_quote('%2C') . '.*?/', '', $string);

regex的另一个替代方法是通过
parse\u str
分解查询字符串

使用第一个
strok
获取基本url并将其分隔,以便可以在
parse_str
中提供查询字符串参数

将其分离并加载到
parse_str
中后,可以对查询字符串的各个部分进行更改。如果你想改变价格,就这样操纵它

使用另一个
strtok
只需有效地修剪
或(
%2C
)之后的字符,然后重新分配

最后,使用
http\u build\u query
重新附加查询字符串,该查询字符串由前面操作中分离的基本url连接

$string = 'test.xyz/builder-list/?price=-200%2C400-500&builder_region=1223%2C3445';
$base_url = strtok($string, '?');
parse_str(str_replace("{$base_url}?", '', $string), $data);
$data['price'] = strtok($data['price'], ',');
$final_string =  "{$base_url}?" . http_build_query($data);
echo $final_string;
$result
将包含:

test.xyz/builder-list/?builder_state=45%2C76&price=-200

如果您使用的是正则表达式,则必须专门捕获
price
,并将分隔符
%2C
的前部分和后部分捕获到单独的正则表达式组中并替换它们。它将如下所示:

preg_replace('/(price\=)([^&]*)%2C[^&]*/', '$1$2', $str)'
               -------- -------             ----
                Grp 1.   Grp 2.              Only grp 1 and 2.
片段:

<?php

$tests = [
        'test.xyz/builder-list/?price=301-500%2C501-1000&builder_country=6442%2C6780%2C6441',
        'test.xyz/builder-list/?price=-200%2C400-500&builder_region=1223%2C3445',
        'test.xyz/builder-list/?builder_state=45%2C76&price=-200%2C400-500',
        'test.xyz/builder-list/?builder_state=45%2C76&price=%2C400-500'
    ];

foreach($tests as $test){
    echo preg_replace('/(price\=)([^&]*)%2C[^&]*/', '$1$2', $test),PHP_EOL;
}

为什么?它只是百分比编码,只需使用
urldecode
,无需常规表达式这里有一个例子@Kevin我只需要字符串URL中
price
查询参数的第一个值,这就是为什么我需要替换URL字符串中
price
query参数中的%2C和其余值的原因,如果它是:我猜test.xyz/builder list/?price=-200&builder\u region=12%2C33是一个拼写错误,应该是test.xyz/builder list/?price=-200&builder\u region=1223%2c3445谢谢它能工作,但是当我有一个URL字符串时,我遇到了一些问题:
http://test.xyz/builder-list/?price=201-500%2C201-500&;builder_country=6356%2C6699
它将转换为:
http://test.xyz/builder-list/?price=201-500&%3Builder\u country=6356%2C6699
当我在浏览器中打印时,它在string@jojo您必须在某处进行双重编码,
&是给htmlah的,对不起!谢谢@vivek_23。我会调整我已经更新了我的代码,所以现在它可以像我认为OP所期望的那样工作:-)嗯。我想你也可以在
if
条件中添加一个
中断
。@vivek_23-是的!不管怎样,它都会起作用,但尽可能正确地做总是更好的:-)我增加了休息时间。再次感谢!一行没有任何麻烦!真棒的解决方案!
<?php

$tests = [
        'test.xyz/builder-list/?price=301-500%2C501-1000&builder_country=6442%2C6780%2C6441',
        'test.xyz/builder-list/?price=-200%2C400-500&builder_region=1223%2C3445',
        'test.xyz/builder-list/?builder_state=45%2C76&price=-200%2C400-500',
        'test.xyz/builder-list/?builder_state=45%2C76&price=%2C400-500'
    ];

foreach($tests as $test){
    echo preg_replace('/(price\=)([^&]*)%2C[^&]*/', '$1$2', $test),PHP_EOL;
}